当前位置: 动力学知识库 > 问答 > 编程问答 >

c - Error with a linked list data structure

问题描述:

I have a problem while compiling this c program, that create a linked list structure and makes the user insert values at the beginning or at the end of the list.

While compiling I get this error:

 list.c: In function ‘list_insert_up’:

list.c:49:6: error: incompatible types when assigning to type ‘list_t’ from type ‘struct list_t *’

*ptr=allocate(ptr);

enter code here

This is the code:

#include <stdio.h>

#include <stdlib.h>

struct list_t{

int val;

struct list_t *next;

};

typedef struct list_t list_t;

list_t *head=NULL;

list_t *curr=NULL;

// FUNCTIONS

list_t*

allocate(list_t *ptr){

return malloc(sizeof(list_t));

}

void

deallocate(list_t *ptr){

free(ptr);

}

list_t*

list_block_create(list_t *ptr, int value){

list_t *curr=ptr;

ptr->val=value;

ptr->next=NULL;

head=curr=ptr;

printf("--Block created successfully!--\n");

}

void

list_print(list_t *ptr){

size_t i=0;

while(ptr!= NULL){

printf("%i\n",ptr->val);

ptr=ptr->next;

}

printf("--List printed successfully!--\n");

}

list_t*

list_insert_up(list_t *ptr,int value){

*ptr=allocate(ptr);

ptr->val=value;

ptr->next=head;

head=ptr;

}

// MAIN

int

main(){

list_t *ptr=allocate(ptr);

list_block_create(ptr,1);

list_print(ptr);

list_insert_up(ptr,2);

list_print(ptr);

return 0;

}

网友答案:

You have to fix a few things:

It is confusing your use of curr, first defining it as a global variable, and then masking it inside list_block_create. This is probably not what you want.

And

list_t*
allocate(){
    return (list_t*) malloc(sizeof(list_t));
}

And

list_t*
list_insert_up(list_t *ptr,int value){
    list_t* ptr2=allocate();
    ptr2->val=value;
    ptr2->next=ptr->next;
    ptr->next=ptr2;
    return ptr2;  /* not really needed, the function might return void */
}

And actually use instead of list_insert_up

list_t*
list_insert_top(int value){
    list_t* ptr=allocate();
    ptr->val=value;
    ptr->next=head;
    head=ptr;
    return ptr;  /* not really needed, the function might return void */
}

And

list_t*
list_block_create(list_t *ptr, int value){
    head=ptr;
    ptr->val=value;
    ptr->next=NULL;
    printf("--Block created successfully!--\n");
    return ptr;  /* not really needed, the function might return void */
}

You would be probably better off changing a little bit the logic of your code, e.g., removing global variables. If you do this, you would probably need to keep the return values of your functions (what I quoted as not needed, as things are now). There are plenty of sample codes out there for linked lists in C.

网友答案:

Here is the problem (origin of the error message):

list_insert_up(list_t *ptr,int value){
    *ptr=allocate(ptr); // left hand: list_t, right hand: list_t *
    ...
}

When you put the * before ptr you dereferencing it. You try to set the value of ptr to a pointer, since allocate() returns a pointer (this riggers the error msg). I don't say there are not other issues in the code.

You need something like this:

list_insert_up(list_t **ptr,int value){
    *ptr=allocate(*ptr);
    (*ptr)->val=value;
    (*ptr)->next=head;
    head=*ptr;
}

Why do you need something like this? Because you want to change the pointer (an address) not the value where the pointer points on.

网友答案:

Change your list_insert_up() function to this:

list_t*
list_insert_up(list_t **ptr,int value){
    (*ptr)=allocate(*ptr);
    (*ptr)->val=value;
    (*ptr)->next=head;
    head=*ptr;
}

then, call this function this way list_insert_up(&ptr,2); in your main() function. You'll get the right answer, because you wanna change the value of a pointer, so you need to pass a pointer pointed to this pointer as argument. Or you can choose to return it instead.

分享给朋友:
您可能感兴趣的文章:
随机阅读: