Nested pointers and references

Asked

Viewed 94 times

4

I’m having a doubt in the interpretation (the way I read my code) on pointer assignments in the C language.

I did not understand the logic of the following assignments:

"If i and j are whole variables and p and q pointers to int, which of the following attribution expressions are illegal?"

Statement of the financial year:

a) p = &i;
b) *q = &j;
c) p = &*&i; 
d) i = (*&)j;
e) i = *&j;
f) i = *&*&j;
g) q = *p;
h) i = (*p)++ + *q;

I am in doubt in items C, D, E and F, because I did not understand the logic of the following attributions... A, B, G and H I have no doubt in the assignments, only those using "*&var".

How do I interpret these assignments in C?

  • Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?

1 answer

2

There’s not much secret if you know how it works * and the & singly.

  • C) take the address of i, then take the contents of this address (which is obviously the value of i) and then take the address of this content and save it in p.
  • D) This syntax is not possible
  • E) Get the address of j and then take the contents of this address to store in i.
  • F) Get the address of j, take the contents of this address and take his address and finally take the contents of this address to store in i.

Interpretation depends on precedence and associativity of operators.

This is done for demonstration. It makes no sense to do this kind of thing. At least not this way.

  • Ahh, I understood how it works... Thanks for the answer!! and, really, I found a bit "absurd" this kind of assignment kkkk

Browser other questions tagged

You are not signed in. Login or sign up in order to post.