-2

I have a struct and in that a struct i have a character pointer but and i am creating different instances of this struct but when i am changing the pointer in one struct the other is also changing.

#include <stdio.h>
#include <stdlib.h>

typedef struct human{
    int age;
    char name[100];
} Human;

int main(){
    FILE *s = fopen("h.txt","r");
    if(s==NULL){
        printf("file not available");
    }

    for(int i=0 ;i<5;i++){
        Human h;

        fscanf(s,"%d",&h.age);
        fscanf(s,"%s",h.name);

        insertintolinkedlist(h);
        // this method is going to insert the human into the linked list
    }

    return 0; 
}

what is happening that all humans in the linked list have different ages but same name!

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
Sara Hamad
  • 645
  • 1
  • 6
  • 12

1 Answers1

1

You need to allocate memory to hold the name.

char* name is just a pointer - it has no memory for saving the name.

You change it to

char name[100];

Remember to check that the names you put into Human.name isn't longer than 100 characters.

To use a linked list you can do something like:

typedef struct human{
    int age;
    char name[100];
    struct human* next;
} Human;

int main()
{
    Human* head = NULL;
    Human* tail = NULL;

    for(.....)
    {
        Human* h = malloc(sizeof(Human));
        if (head == NULL) head = h;
        if (tail != NULL)
        {
            tail->next = h;
        }
        tail = h;
        h->next = NULL;
        h->age = ....;
        strncpy(h->age, "..name..", 100);
    }

    // ..... other code

    // Remember to free all allocated memory
}
Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
  • thank you you are right but even if i do this the pointer still point to the same memory location – Sara Hamad Mar 05 '16 at 07:52
  • i am always doing a human and then adding it to the linked list but this human is going to have diiffernernt age but same name – Sara Hamad Mar 05 '16 at 07:53
  • @SaraHamad - you don't have any linked list! – Support Ukraine Mar 05 '16 at 07:54
  • imagine this code is in a loop and i am always making humans and adding to linked list these humans have different ages but when i change the name it change for all humans thanks a lot – Sara Hamad Mar 05 '16 at 07:54
  • here i only showed a part of my code for simplicity but what i really am doing is making the struct and then calling a function that is adding it to the linked list – Sara Hamad Mar 05 '16 at 07:55
  • How do you make new humans? `malloc` ? You should probably show that code as well. – Support Ukraine Mar 05 '16 at 07:58
  • i edited the code , please note that i am new in c and thank you for your hellp much appreciated – Sara Hamad Mar 05 '16 at 08:06
  • You don't get a new variable in each loop using `Human h;` inside the loop. You must allocate it dynamic using malloc. – Support Ukraine Mar 05 '16 at 08:11
  • the name is read from i file how can i use strcpy in this case? – Sara Hamad Mar 05 '16 at 08:13
  • I'm not using `fscanf` much but I think you can do `fscanf(s, "%s", h->name);` instead of a strcpy. However, it is not good because you can't make sure the name is shorter than 100 characters. However, take a look at this: http://stackoverflow.com/questions/12306591/ensure-scanf-only-reads-so-many-characters-in-a-string Maybe you can do `fscanf(s, "%100s", h->name);` – Support Ukraine Mar 05 '16 at 08:20