Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > problem to undestand nested pointers

Reply
Thread Tools

problem to undestand nested pointers

 
 
happy
Guest
Posts: n/a
 
      01-14-2010
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?
 
Reply With Quote
 
 
 
 
Tom St Denis
Guest
Posts: n/a
 
      01-14-2010
On Jan 14, 8:12*am, happy <(E-Mail Removed)> wrote:
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointers, pointers, pointers... cerr C Programming 12 04-07-2011 11:17 PM
Nested friend class in nested template problem tonvandenheuvel@gmail.com C++ 3 12-07-2007 03:02 PM
I don't undestand why sometime python (or other) add "tmpWvk7zy"(tmp + random string) after "buildout-eggs" in path of python script KLEIN Stephane Python 0 09-24-2007 08:47 PM
pointers to pointers // exception handling error muser C++ 3 09-18-2003 06:19 PM
Template specialization of pointers with function pointers Phil C++ 1 09-16-2003 02:17 AM



Advertisments