Re: More on pointers to pointers.
- From: Skarmander <invalid@xxxxxxxxxxxxxx>
- Date: Fri, 31 Mar 2006 20:45:48 +0200
Skarmander wrote:
Dave Vandervies wrote:Evaluation of the indirection, obviously.In article <1143773007.173184.158940@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,<snip>
Chad <cdalten@xxxxxxxxx> wrote:
To be precise in a way that doesn't really matter but is still notable: this is a direct consequence of how & is defined. In particular, the standard guarantees that "&*p" is equivalent to "p" for *any* pointer p, even if p is invalid, because the subexpression "*p" is not evaluated.I guess even more to the point, why is this legal?
arg = &(*ptr) ;
ptr gets converted to a pointer to its first element, and then you follow
that pointer, so the expression inside the parens identifies an object
of type pointer to char.
Then the & operator gives you a pointer to that object, with type
pointer to pointer to char. Since this is the same type of the object
you're storing it into, this works with no problems or complaints from
the compiler.
(This one is actually a special case of a more general rule: since & (get
pointer) and * (follow pointer) are inverse operations, saying "&(*p)"
will always give you the same value as just saying "p" for any pointer p,
and "*(&x)" will always give you the same object as just saying "x".)
Some compilers actually get this wrong if optimization is not turned on, and will insert evaluation of the pointer.
S.
.
- References:
- More on pointers to pointers.
- From: Chad
- Re: More on pointers to pointers.
- From: Dave Vandervies
- Re: More on pointers to pointers.
- From: Skarmander
- More on pointers to pointers.
- Prev by Date: Re: What will happen if main called in side main function?
- Next by Date: Re: What will happen if main called in side main function?
- Previous by thread: Re: More on pointers to pointers.
- Next by thread: Something About Date Type
- Index(es):
Relevant Pages
|
|