简体   繁体   中英

Why does my linked list head point to 2 items before the head

So im trying to practice my C double pointer linked list in not global form and im confused why s--which is practically head-- points to null first then some random address even though i think i moved it to the first node in the list.

here is my code:

typedef struct nodeStruct{
    int               item;
    struct nodeStruct *next;
} Statistician;

void add(Statistician **s, int x);
void displayData(Statistician **s);

int main(int argc, char *argv[]) {
    Statistician *s = NULL;

    add(&s, 3); 
    add(&s, 4);
    add(&s, 5);
    add(&s, 6);
    add(&s, 7);
    add(&s, 8);
    add(&s, 9);
    displayData(&s);

    return 0;
}
void add(Statistician **s, int x){
    Statistician *temp = malloc(sizeof(Statistician));

    temp->item = x;
    temp->next = NULL;

    if(s == NULL){
        s = &temp;
    }
    else{
        Statistician *travel = s;

        while(travel->next!=NULL){
        travel = travel->next;
    }

    travel->next = temp;
   }
 }

 void displayData(Statistician **s){
    Statistician *temp = s;

    printf("List is: ");

    while(temp!=NULL){
    printf("%d ", temp->item);
    temp = temp->next;
    }
 }

I get this output from my code and i also get these warnings: List is: 0 43586480 3 4 5 6 7 8 9 [Warning] initialization from incompatible pointer type [enabled by default] at this line of code Statistician *travel = s

i could just always move the displaydata two times before printing the data so that the first to that i dont want to see wont go out but i wanna know why its working like that. also i could also ignore these errors but i wanna learn how to fix it.

[Warning] initialization from incompatible pointer type [enabled by default] at this line of code Statistician *travel = s

This says you are assigning incompatible pointer types.

if(s == NULL){
    s = &temp;
}

Here s is not pointing to your head , *s is. So it should be

if(*s == NULL){
*s = temp;
}

Then

Statistician *travel = s;

Again it should be

Statistician *travel = *s;

Then in displayData()

void displayData(Statistician **s){
Statistician *temp = *s;

The code has wrong logic. you didn't use s properly. it would be something like. Your earlier operations didn't do anything significant. You made some wrong changes to a local variable and assigned a single pointer value to a double pointer.

void add(Statistician **s, int x){
    Statistician *temp = malloc(sizeof(Statistician));

    temp->item = x;
    temp->next = NULL;

    if(*s == NULL){
        *s = temp; //<---change 
    }
    else{
        Statistician *travel = *s; //<--change

        while(travel->next!=NULL){
        travel = travel->next;
       }

    travel->next = temp;
   }
 }

You need to check whether malloc call fails or not by checking it's retyurn value. This will save you from undefined behavior when the call fails.

Also displayData function is also wrong. It would be

 void displayData(Statistician **s){
    Statistician *temp = *s; //<---change

    printf("List is: ");

    while(temp!=NULL){
    printf("%d ", temp->item);
    temp = temp->next;
    }
 }

Earlier you had undefined behavior accessing memory that you didn't even allocate or you don't have permission to. This invoked Undefined behavior in your code.

Clearing the idea:

Here when you pass &s then that means address of s is being sent, a local variable having type Statistician** will hold that. Now if you don't dereference it, then you simply play with the address of s which is contained in local variable. This is not what you want. You need to make changes to the address that is being passed. So you do it by dereferencing it *s = temp and like this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM