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;
CodePudding user response:
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;
CodePudding user response:
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.