Error: incompatible types when assigning the type "value" from the type "void *"?

I get this error when I try to malloc some memory for my ints structure.

typedef struct _values {
random ints;
} values;

I tried the lines below, but my compiler doesn't like this. How to fix the error?

values v;
v = malloc(sizeof(values));
+2
source share
3 answers

You forgot to add an asterisk (*) after valuesand before vto mark it as a pointer:values *v;

, , v ( ) . values. malloc . Sine , void * - - void * struct, do, , .

+4

malloc ; values v; ( " " ). , .

" ", , v ; :

values *v;
v = malloc(sizeof(values));

free, .

+3

values v;

This is an instance of the structure allocated on the stack.

v = malloc(sizeof(values));

malloc returns a pointer void*, and the compiler will not allow you to assign a pointer to an instance.

you need to declare a pointer, and then assign it a malloc return pointer.

sort of

  values ​​* v = NULL;

v = malloc(sizeof(values));

+2
source

All Articles