2

I am having a problem with my code to convert from 'void *' to 'NODE *'

Can anyone help me get around this problem?

NODE *list_create(void *data)
{
NODE *node;
if(!(node = malloc(sizeof(NODE)))) return NULL;
node->data=data;
node->next=NULL;
return node;
}

I get the error right underneath the = sign. Any help would be greatly appreciated!

mebrunner24
  • 65
  • 1
  • 2
  • 7
  • 3
    What is the error? May be you are using a C++ compiler instead of a C compiler. – sgarizvi Nov 16 '13 at 15:30
  • 1
    No C compiler would complain about the absence of a cast in the assignment; every C++ compiler would complain about the absence of a cast in the assignment. Therefore, you should start compiling your code with a C compiler, not with a C++ compiler, because you will be confused every time the compiler complains about something that is legitimate in C but illegitimate in C++. Using the wrong compiler is a serious problem. (Are you working on Windows, perchance?) – Jonathan Leffler Nov 16 '13 at 15:52

2 Answers2

2

You have not specified the error, but assuming from the looks of the code, most probable is the type conversion error, something like:

Cannot convert void* to NODE*

The question is tagged with C and the code written here is also valid C, but invalid C++. So if there is an error, it can be safely assumed that a C++ compiler is being used to compile the code.

To remove the error, either use a C compiler; or if you want to stick with a C++ compiler, explicitly type cast the return value of malloc.

if(!(node = (NODE*)malloc(sizeof(NODE)))) return NULL;
sgarizvi
  • 16,623
  • 9
  • 64
  • 98
  • If you're using a C++ compiler, it would be better to stick to writing C++ code, using native C++ idioms. Although C++ does support `malloc()`, it is not the normal way of allocating memory in C++ — you should be using `new` and `new[]` and related idioms. – Jonathan Leffler Nov 16 '13 at 15:53
  • @JonathanLeffler... Yes, I would also recommend using `new` instead of `malloc`, but I wanted to stay close to OP's code. – sgarizvi Nov 16 '13 at 16:03
  • That made the error go away. Thank you. As for using a C compiler I am just using Microsoft Visual C++ 2010 Express. It was free from Microsoft.. Is there a different program that is free that is for just C? – mebrunner24 Nov 16 '13 at 16:04
  • @mebrunner24... You can use Visual C++ as a `C++` compiler as well as a pure `C 89` compiler. Just go to `Project Properties` -> `C/C++` -> `Advanced` -> `Compile As`->`C`. – sgarizvi Nov 16 '13 at 16:07
  • Will that automatically be set every time I open it? I am curious because I can never get a program to run correctly unless I go to Project -> Project Properties -> Linker -> General and change the Enable Incremental Linking to NO (/INCREMENTAL:NO).. Would changing it to a C compiler get rid of this annoying error as well? – mebrunner24 Nov 16 '13 at 16:11
  • @mebrunner24... No this setting has nothing to do with linker. Once you select the compiler, it will be saved permanently for the project until you change it. I cannot say anything about the linker problem you are having. – sgarizvi Nov 16 '13 at 16:14
0
if(!(node = malloc(sizeof(NODE)))) return NULL;

malloc returns a void pointer (as it doesn't know what you want to do with the memory you've requested.

The compiler is trying to figure out why you want the memory. Cast it to a (NODE *) to give it a hint

davbryn
  • 7,156
  • 2
  • 24
  • 47