1.11M Members

help me with pointer to pointer indirection

 
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++;
}
}
 
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.

 
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

 
0
 

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

 
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 six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: