Originally Posted By: AndrewAMD
This doesn't work in normal C either.

s.P = malloc(sizeof(int));

Visual Studio says...
"a value of type "void *" cannot be assigned to an entity of type "int *"

Sounds like you compiled with C++ compiler.
C performs an implicit cast, whereas C++ requires an explicit cast for void* (static_cast<int*>)

Compiles in C

Code:
#include <stdlib.h>
#include <stdio.h>

struct S
{
	int* P;
};

int main()
{
	struct S s;
	s.P = malloc(sizeof(int));
	*(s.P) = 1234; // Ok
	s.P[0] = 1234; // Ok
	int* p = s.P;
	p[0] = 2345; // Ok
	printf("%d", *(s.P));
	free(s.P);
	return 0;
}


Last edited by pascalx; 09/03/17 14:40.