Show the hex addresses and variable values after the statements have been executed. (All pointers are 4 bytes!) The first byte of memory below is xFF2A.

Not too sure if I am doing this right. Please help. Thanks.

t s r q x d c b a
8 8 4 4.5 5 4 32.5

int main()
{
    float a= 32.5, b;
    int c = 5, d = 4.5, x;
    float  *q, *r;
    int *s, *t;
    q = &a;
    r = &b;
    s = &x;
    t = &d;
    *r = c%d;
    *s = *t + c;

The exercise seems to be one for paper and pencil. The first variable (a) has a hypothetical address of 0xFF2a. You're to determine the addresses of the other variables, and also work out the values of the pointers and variables after all of the listed statements are "executed". For example, after line 7 is executed (and assuming that both float and int are 4 bytes) you'd have this:

a (0xFF2a): 32.5
q (0xFF4a): 0xFF2a
    *q (0xFF2a): 32.5

I got the address of 0xFF4a by adding 20 bytes (5 variables at 4 bytes each) to the base address. You can follow a similar process for each of the other variables to get its address.

I am still kind of confused. Can you o into more details please. thanks

I am still kind of confused.

Consider speaking with your teacher instead of looking for help online. I could clear up your confusion on this topic easily with a little bit of hand waving and a whiteboard, but it's tedious to no end using forum posts.

Can you o into more details please

It really boils down to this: The address of the variable a is 0xFF2a. If the size of the variable a is 4 bytes then the address of the next variable (b in this case) is 0xFF2a + 4, or 0xFF2e. Repeat the process to get the address of every variable in the program.

Once you have the address of every variable, learn how pointers work and then use paper and pencil to write down the result of each executable statement in the program. If you know how pointers work, it's trivial. If you don't, it's impossible.

Yes I will ask my teacher, he has not gone over this yet and was trying to understand it alone first. I jsut want to know if I am going in the right direction with this.

t s r q x d c b a
4 0xFF2e 8 0xFF2a 4 4.5 5 4 32.5

Each variable has both an address and a value.
After line 3 above executes, you have:

  • a (address = 0xFF2A, value = 32.5)
  • b (address = 0xFF2E [since each float takes 4 bytes], value = unknown [*])

after line 4 executes, you have:

  • a (address = 0xFF2A, value = 32.5)
  • b (address = 0xFF2E, value = unknown [*])
  • c (address = 0xFF32, value = 5)
  • d (address = 0xFF36 [assuming each int also takes 4 bytes], value = 4 [do you know why, or can you guess?])
  • x (address = 0xFF3A, value = unknown [*])

And so on...

(*) Depending on your compiler, it might be 0.0 (0 for ints, NULL for pointers) or the memory might be full of garbage, use whatever answer your teacher says is right for your case ... or type in the lines and then print out the values afterwards, and see for yourself what happens when you don't initialize variables! Still generally better (and more readable) to explicitly initialize variables before using them, rather than relying on potentially-compiler-specific behavior.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.