This code:
- void *test = NULL;
-
-
(int *)test = malloc(...) <- lvalue error
-
won't compie because you are assigning a void* to an int*. You can assign an int* to a void* but not the other way around.
This code:
- void *test = NULL;
-
-
test = malloc(sizeof((int *)test); appeared ok
-
*((int *)test) = 10;
-
You are allocating the size of an int address.
Then you assign the void* from malloc to a void* test. That's OK.
Then you cast a void* to an int*, dereference the int* and assign an int value. WARNING: You allocated the size of an address and not the size of an int. This works as long as the int value is the same size (or smaller) than an int address.
This code:
- void *test = NULL;
-
test = malloc(sizeof(*((int *)test)); appeared ok
-
*((int *)test) = 10;
-
allocates the size of an int. Then the void* from malloc is assigned to the void* test. That's OK.
Then the void* test is cast to an int*. That's OK.
Then the int* test is dereferenced to an int and an int value assigned. That's OK.
Just look at these things the way the compiler would look at it.