0

Assume one dimensional array a={1,2,3,4} with base address as 100.Find the value of p.

P=&a[0];

I know it will give tha base address of 0th element of 1D array. But in which order c compiler will evaluate this in first way or second?

FIRST:

P=&a[0];
P=&*(a+0);
P=&*a;

Or Second:

P=&a[0];
P=*&(a+0);
P=*&a;
Roshan Rai
  • 11
  • 2
  • I propose to use more pairs of `( )` to make your question clear. Also, I have to admit that prescisely asked, the question as I understand it, has so obvious an answer that I doubt I got your point. I recomennd to spend more English prose on explaining what puzzles you. Maybe describe the consequences of either answer for your thinking or for whatever you are trying. Especially, to help with my confusion, please explain what do you imagine `&(a+0)` would even mean. – Yunnosch Jun 03 '22 at 05:53
  • Why do you give the base address and not the `sizeof` array element? – meaning-matters Jun 03 '22 at 06:05
  • @Yunnosch &(a+0) means base address of entire 1d array assuming a is one dimensional array. – Roshan Rai Jun 03 '22 at 06:09
  • It's really easy to look up operator precedence in any C book or online source. I even made an attempt to write one for SO long time ago https://stackoverflow.com/q/17369090/584518. Typing "c operator precedence table" in Google spits out some 20 tables or so. – Lundin Jun 03 '22 at 06:28

2 Answers2

1

Postfix operators bind stronger than prefix operators. So P = &a[0]; is parsed as

P = &(a[0]);

Which can be transformed as

P = &(*(a + 0));

Equivalent to

P = &(*a);

Also equivalent to

P = &*a;

If a is a pointer or an array, the expression ultimately boils down to:

P = a;
chqrlie
  • 131,814
  • 10
  • 121
  • 189
0

I am unclear what you are actually asking, because the answer to the question I read is so obvious.
I will just phrase my thoughts on your shown codes.

P=&a[0];, probably the expression you want to discuss.
P=&*(a+0);, probably your first step of interpretation, but inconsequently ()ed.
P=&(*(a+0));, my guess of what you would write if you were more rigorously ()ing. P=&*a;, unclear, does not help in the analysis. I'll ignore it.

Lets look at the inner *(a+0); an expression including a, which probably is an array or a pointer to something. a+0 can be used as a pointer to the same. *(a+0) would dereference that pointer, getting you the first element as operand for further steps. &(...) then gets you the address of that first element.
OK, makes sense.

The alternative would be:

P=*(&(a+0));, with an inner &(a+0).
This is the expression (a+0), which could be used as a pointer (see above) but not as a pointer variable. I.e. it cannot be assigned to itself and it does not have an address which can be expressed with &(...). That is where this path ends.

So one path which makes sense, one which does not, seems clear to me.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
  • Let me know in case my "obvious" is exactly where I get everything wrong. Maybe just trying to compile gets you interesting errors or warnings, but without a [mre] I can't. – Yunnosch Jun 03 '22 at 06:10