SPLASH SPLASH - 1 year ago 112
C Question

Malloc invalid conversion from 'void*'

I don't know much about malloc and I know there are similar questions but I still wanted to ask.

When I compiled my code I came across these errors

scheduler.c: In function 'char* addprocess(char*, FILE*)':
scheduler.c:127:60: error: invalid conversion from 'void*' to 'process*'
scheduler.c:135:38: error: invalid conversion from 'void*' to 'char*'
scheduler.c:170:65: error: invalid conversion from 'void*' to 'event*'
scheduler.c: In function 'int main()':
scheduler.c:381:52: error: invalid conversion from 'void*' to 'stat*'
*** Error code 1
make: Fatal error: Command failed for target `all'

Here are the lines from the code.

127 struct process* newproc = malloc(sizeof(struct process));
135 newproc->name = malloc(line-pos-1);
170 struct event* newevent = malloc(sizeof(struct event));
381 statistics = malloc(amount* sizeof(struct stat));

What does it mean when it say invalid conversion and why. Also what is the best solution to these errors.

Answer Source

The problem is that you're using a C++ compiler to compile C code.

In C, a void * can freely be converted to or from any other non-function pointer without a cast. In C++, a cast is required, which is why you're getting errors about converting to/from void *.

Use the C compiler to compile C code:

gcc scheduler.c -o out
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download