1,105,534 Community Members

help me with pointer to pointer indirection

Member Avatar
linkingeek
Newbie Poster
9 posts since Jul 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Could you please tell pictorially difference bw these 2 codes(o/p is same) and suggest any source where i can once in for all resolve doubts in pointer to pointer problems except "K&R in ANSI C"

#include <stdio.h>
   #include <string.h>
 void pointer(char**);
   void main()
{
char *p[]={"name","fame","claim"};

pointer(p);
}
void pointer(char **a)
{int i=0;
while(a[i]!=NULL)
{

printf("\nstring%d is:%s \n",i,a[i]);
i++;
}
}

or

#include <stdio.h>
   #include <string.h>
 void pointer(char**);
   void main()
{
char *p[]={"name","fame","claim"};

pointer(&p);
}
void pointer(char **a)
{int i=0;
while(a[i]!=NULL)
{

printf("\nstring%d is:%s \n",i,a[i]);
i++;
}
}
Member Avatar
Ancient Dragon
Achieved Level 70
27,643 posts since Aug 2005
Reputation Points: 5,232 [?]
Q&As Helped to Solve: 3,038 [?]
Skill Endorsements: 115 [?]
Team Colleague
Featured
Sponsor
 
0
 

The second program is wrong because line 8 is not passing a pointer to pointer, but pointer to pointer to pointer, which means that function pointer should have three stars, not two.

Member Avatar
linkingeek
Newbie Poster
9 posts since Jul 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

but y its producing a same output thats actually confusing me there should be some garbage o/p but prog is running fine
well i'm compiling it with turbo c what changes i should make to it to compile on gcc

Member Avatar
Ancient Dragon
Achieved Level 70
27,643 posts since Aug 2005
Reputation Points: 5,232 [?]
Q&As Helped to Solve: 3,038 [?]
Skill Endorsements: 115 [?]
Team Colleague
Featured
Sponsor
 
0
 

Change second example program like this: pointer(&p[0]); Also add NULL to this line: char *p[]={"name","fame","claim",NULL};

Member Avatar
versan
Newbie Poster
23 posts since Nov 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Is it the code with which you have doubt or you don't understand pointer to pointer concept??

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: