1

Below is the code of an example of structure. When I add multiple information for different person all the time it shows output same the last inserted information. How can I fix it?

#include<stdio.h>
#include<string.h>

struct Person{

       char *name;
       char *adress;

}p[100];

void insert(int ind , char *name, char *adress){

      p[ind].name = name;
      p[ind].adress = adress;
}

void display(int n){`enter code here`

   for(int i =0 ; i<n ; ++i){
     printf("%s %s\n" , p[i].name , p[i].adress);
   }
}


int main(){
char name[100] , address[100];

    for(int i = 0 ; i<2 ; ++i){

         fflush(stdin);
         gets(name);
         fflush(stdin);
         gets(address);
         insert(i , name , address);
    }


   display(2);


  return 0;
}
Turjo
  • 11
  • 4
  • 2
    You never make a copy of the name and address in `insert`; you just point the pointer elements to the two arrays. Those two arrays will always take the last values you entered, so all the struct elements always show those last two values. – 9769953 Jan 15 '22 at 07:57
  • To make a copy, you can use a function like `strdup`: `p[ind].adress = strdup(adress);` etc. Just make sure to check that the returned value of `strdup` is not `NULL`. – 9769953 Jan 15 '22 at 07:58
  • For showing all the information what should I do ? – Turjo Jan 15 '22 at 07:59
  • There are other problems with your code as well, as it will fail (or cause severe problems) for names and addresses of 100 or more characters (and yes, those exist in the world). I think `gets` is also not recommended; `fgets` (with `stdin`) would be better. – 9769953 Jan 15 '22 at 07:59

1 Answers1

0

For storing and outputting all the info, you need to provide separate memory for them.
The shown code always overwrites the same buffer and stores only pointers to that buffer.
A small change your structure definition makes sure that memory is provided instead of only storing a pointer (to always the same memory).

Then read into the individual memory, that also saves the otherwise necessary copying of C-type strings, which is tedious.

The following code shows all info by using the described concepts:

#include<stdio.h>
#include<string.h>

struct Person{

       char name[100];
       char adress[100];

}p[100];

void display(int n){

   for(int i =0 ; i<n ; ++i){
     printf("%s %s\n" , p[i].name , p[i].adress);
   }
}


int main(){

    for(int i = 0 ; i<2 ; ++i){

         // fflush(stdin); Not using this is a habit you should adopt.
         // gets(name); Not using this is a habit you should adopt.
         fgets(p[i].name, 99, stdin);
         // fflush(stdin); Not using this is a habit you should adopt.
         fgets(p[i].adress, 99, stdin);
    }


   display(2);


  return 0;
}

For an input of e.g.:

name1
address1
name2
address2
a
b
c
d
e
f
g
h

(with intentional additional input after the expected one, to show clean ending)

The output is as you requested, with different infos instead of always the same.

name1
 address1

name2
 address2

You might want to replace the newline which is read in along with the input.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54