Pointer to pointer
James Kuyper said:
>
The "%p" format specifier expects a void* argument. This is one of the
few places where a cast is routinely needed and perfectly correct:
>
printf("*a=%p *p=%p %c %s\n", (void*)*a, (void*)*p, **p, *p);
printf("*a=%p *p=%p %c %s\n", *a, *p, **p, *p);
Ben Bacarisse said:
Technical matter: %p expects arguments of type void * and the
conversion is not done automatically so you need to write:
>
printf("*a=%p *p=%p %c %s\n", (void *)*a, (void *)*p, **p, *p);
I'm not sure James and Ben have this right, but I'd say it's more
probable I'm wrong here. Still, we'll see. CLC psychology dictates
that if I'm wrong, a dozen people with a grudge will be quick to
correct me.
6.2.5 (27) says the following:
"A pointer to void shall have the same representation and alignment
requirements as a pointer to a character type."
And then a footnote for that says:
"The same representation and alignment requirements are meant to
imply interchangeabil ity as arguments to functions ..."
The OP is passing a (char *) to %p. Therefore, he doesn't need to
cast the printf() arguments to (void *).
This is the Unix execl() case in reverse. The function execl(),
for those terminally in ANSI/ISO C land, uses a null character
pointer - i.e., (char *)0 or (char *)NULL - to indicate the end
of its variable-length argument list.
execl("/path/file", "arg", "arg", "arg", NULL);
works if NULL is defined as ((void *) 0), whereas it's not
guaranteed to work if NULL is defined as 0.
Yours,
Han from China
James Kuyper said:
> printf("*a=%p *p=%p %c %s\n", *a, *p, **p, *p);
The "%p" format specifier expects a void* argument. This is one of the
few places where a cast is routinely needed and perfectly correct:
>
printf("*a=%p *p=%p %c %s\n", (void*)*a, (void*)*p, **p, *p);
printf("*a=%p *p=%p %c %s\n", *a, *p, **p, *p);
> printf("*a=%p *p=%p %c %s\n", *a, *p, **p, *p);
conversion is not done automatically so you need to write:
>
printf("*a=%p *p=%p %c %s\n", (void *)*a, (void *)*p, **p, *p);
probable I'm wrong here. Still, we'll see. CLC psychology dictates
that if I'm wrong, a dozen people with a grudge will be quick to
correct me.
6.2.5 (27) says the following:
"A pointer to void shall have the same representation and alignment
requirements as a pointer to a character type."
And then a footnote for that says:
"The same representation and alignment requirements are meant to
imply interchangeabil ity as arguments to functions ..."
The OP is passing a (char *) to %p. Therefore, he doesn't need to
cast the printf() arguments to (void *).
This is the Unix execl() case in reverse. The function execl(),
for those terminally in ANSI/ISO C land, uses a null character
pointer - i.e., (char *)0 or (char *)NULL - to indicate the end
of its variable-length argument list.
execl("/path/file", "arg", "arg", "arg", NULL);
works if NULL is defined as ((void *) 0), whereas it's not
guaranteed to work if NULL is defined as 0.
Yours,
Han from China
Comment