Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • 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!

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

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...If there has ever been a justification needed for access to the net during working hours, just referring to this site should suffice. Fantastic!..."

Geography

Where in the world do Tek-Tips members come from?

pointers + pointers to pointers

gander (Programmer)
7 Feb 01 13:44
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
Lim (Programmer)
7 Feb 01 15:39
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);
    }

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!

Back To Forum

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