I'm having trouble with malloc. I copied this from a tutorial and tried to run it but it didn't work. It is supposed to insert a new element in a linked list (when I call it from main).
llist ajouterEntete(llist liste, int valeur)
element* nouvelElement = malloc(sizeof(element));
nouvelElement->val = valeur;
nouvelElement->nxt = liste;
return nouvelElement;
}
I get this error message:
list.C: In function `element* ajouterEntete(element*, int)':
list.C:25: error: invalid conversion from `void*' to `element*'
I then tried to change the line to:
element * nouvelElement = (element *) malloc(sizeof(element));
I don't get any error messages but it doesn't work.
sorry my fault!
i want you to add it after: nouvelElement->nxt = liste;
you add a new node to the head of the list don't you i mean that's what that piece of code does
isn't it?
Also i want to ask what happens with the return value nouvelElement
what does the program you returned it do with it?
This code that you are using seems to be using a non-standard C-based linked list created by the maker of the tutorial. Because of this, I'm positive that you are using a C compiler instead of a C++ compiler which doesn't allow for the use of new (without the proper explicit linking and declarations). C++ is heavily based on C but I want to make sure that you understand that they are definitely two different languages.
Thank you everyone. I'm using a C++ compiler. Not sure why the malloc was not working (I mean not sure what I was doing wrong!!!), but I figured it out with new.