1

I'm rather new to c, and I'm trying to implement a linked list. I wrote this:

struct List;
typedef struct List* ListRef;
struct List{
        void *data;
        ListRef next;
        ListRef last;
};

ListRef newList(void* headData);
[...]

ListRef append(ListRef list, void* data){

        ListRef newlist = newList(data)

        list->last->next = newList; //here I get a warning
        list->last = newList;  //here I get a warning

        return newList;
}

newList is compiled with no warnings. In the two lines with the comments I get:

warning: assignment from incompatible pointer type

What am I doing wrong?

Thank you!

2
  • 1
    There is semicolon missing after: newList(data). Commented Dec 28, 2014 at 10:45
  • 5
    Seriously consider a different name for your local automatic variable newList than the same name as the function newList(). Unless you're purposely trying to obfuscate code, it is a recipe for errors. Commented Dec 28, 2014 at 10:47

1 Answer 1

2

Change

    list->last->next = newList; //here I get a warning
    list->last = newList;  //here I get a warning

    return newList;

to

    list->last->next = newlist; //here I get a warning
    list->last = newlist;  //here I get a warning

    return newlist;

Analysis:

You have a function named newList, which you have mixed-up with the instance of structure you created(newlist).

You sure dont code like you are new to C. :) Hope this helps

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.