problem to undestand nested pointers

H

happy

Hello all, Below is a code snippet in C

#include<stdio.h>
int main (void)
{
static char *s[ ] = {“black”, “white”, “yellow”, “violet”};
char **ptr[ ] = {s+3, s+2, s+1, s}, ***p;
p = ptr;
**++p;
printf(“%s\n”,*--*++p + 3);
return 0;
}

I am getting some problem in understanding output of this problem.
Here initially, p contains
address of first element of ptr i.e. &(s+3), then due to ++p, p is &(s
+2).
Now I can't understand how the term *--*++p in printf is evaluated.

Please Help me understand.
Is there any good way to understand these type of nested pointers?
 
T

Tom St Denis

Hello all, Below is a code snippet in C

#include<stdio.h>
int main (void)
{
static char *s[ ]  = {“black”, “white”, “yellow”, “violet”};
char **ptr[ ] = {s+3, s+2, s+1, s}, ***p;
p = ptr;
**++p;
printf(“%s\n”,*--*++p + 3);
return 0;

}

 I am getting some problem in understanding output of this problem.
Here  initially, p contains
address of first element of ptr i.e. &(s+3), then due to ++p, p is &(s
+2).
Now I can't understand how the term *--*++p in printf is evaluated.

Please Help me understand.
Is there any good way to understand these type of nested pointers?

Don't write code like that? But that being said use your order of
precedence and left-right associativity to work it out. Break it
down...

++p; // pre increment p
*++p; // pre increment p, then derefence
--*++p; // pre increment p, then derefence, then pre-decrement *++p
*--*++p; // ditto then dereference

You could write *--*++p ignoring the effects as

p[1][-1]

where p[1] points to s+1, (s+1)[-1] points to "black", and "black" + 3
resolves to "ck\0"

And indeed the output is "ck\n" as the printf does.

btw the ** on the **++p; statement doesn't do anything [useful].
Basically you're preincrementing p then dereferencing it two orders.
e.g. **++p == "white"

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top