×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

pointers + pointers to pointers

pointers + pointers to pointers

pointers + pointers to pointers

(OP)
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("\nEnter Names for person %d ",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("\n");
      puts(*n_ptr++); //This is where the program crashes
      }               //It compiles alright though
           
    }
I have tried several combinations but no joy

RE: pointers + pointers to pointers

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("\nEnter the number of people > ");
 scanf("%d",&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("\nEnter Names for person %d ",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("\n");
      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("\nEnter the number of people > ");
 scanf("%d",&people);
 
      name=malloc(people*21);
        
      for(i=0;i<people;i++)
         {
          printf("\nEnter Names for person %d ",i+1);
          fflush(stdin);
          gets(*(name+i));
      }


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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close