Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pointers + pointers to pointers

Status
Not open for further replies.

gander

Programmer
Aug 11, 2003
5
US
having problems with output using pointers
void main()
{
int people,
i,j;
char *name[21],
**n_ptr=name;

printf("\nEnter the number of people > ");
scanf("%d",&people);

for(i=0;i<people;i++)
strcpy(*n_ptr,NULL);
is this required?

for(i=0;i<num_students;i++)
{
printf(&quot;\nEnter Names for person %d &quot;,i+1);
fflush(stdin);
gets(*n_ptr); //if I put the puts(*n_ptr here
**n_ptr++; // it works (but thats not what I
} //want)



for(j=0;*name;j++) //loop till null char is reached
{
printf(&quot;\n&quot;);
puts(*n_ptr++); //This is where the program crashes
} //It compiles alright though

}
I have tried several combinations but no joy
 
Let see what you are doing....
void main()
{
int people,
i,j;
char *name[21], /* Lim: allocated 21 pointer to char ??? */
**n_ptr=name;

printf(&quot;\nEnter the number of people > &quot;);
scanf(&quot;%d&quot;,&amp;people);

for(i=0;i<people;i++)
strcpy(*n_ptr,NULL);
/* Lim: I am surprised it did not crush here, anyway I can not imagine what you was hoping to do by this */

for(i=0;i<num_students;i++)
{
printf(&quot;\nEnter Names for person %d &quot;,i+1);
fflush(stdin);
gets(*n_ptr); //Lim: It is not an ptr to array it is just wild pointer. You have to allocate this array before

**n_ptr++; // it works (but thats not what I
} //want)



for(j=0;*name;j++) //loop till null char is reached
{
printf(&quot;\n&quot;);
puts(*n_ptr++); //This is where the program crashes
} //It compiles alright though

}
If 20 is a max lenght of name do next:

include <stdio.h>
include <stdlib.h>
void main()
{
int people,i;
char (*name)[21] = NULL; /* Lim: ptr to array of 21 char */

printf(&quot;\nEnter the number of people > &quot;);
scanf(&quot;%d&quot;,&amp;people);

name=malloc(people*21);

for(i=0;i<people;i++)
{
printf(&quot;\nEnter Names for person %d &quot;,i+1);
fflush(stdin);
gets(*(name+i));
}


for(j=0;j<people;j++) //loop till null char is reached
{
printf(&quot;\n&quot;);
puts(*(name+i));
}
free(name);
}

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top