I get this error when I'm trying to malloc some memory for my struct of ints.
typedef struct _values {
random ints;
} values;
I have tried the lines below but my compiler doesn't like it. How do I fix the error?
values v;
v = malloc(sizeof(values));
You forgot to add the asterisk (*) after the values and before the v to mark it as a pointer: values *v;
The way you set it now, the v (without the asterisk) is defined as a stack variable and would be allocated on the stack and discarded once the function ends. It's type will be simply values.
malloc is used to allocate memory on the heap and returns a pointer to the memory. Sine the function has no way of knowing the type it returns it as a void * type - Which gives you your error - you're attempting to assign a void * type into a struct type, which the compiler can't do, nor can the compiler find a legitimate cast that could resolve the problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With