I have a void* variable which I am getting through a socket connection. I need to cast this to a struct type, wich is defined both on the client and server side.
I have provided a sample of the assignment in question in the code below. In the example I am leaving out the networking code for the sake of briefness.
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int x;
int y;
} testStructType;
void someFunction(void* p)
{
//some processing goes here.
}
int main(void) {
testStructType testStruct;
void* p;
p=malloc(sizeof(testStructType));
someFunction(p);
testStruct=p;
printf("%i ,%i",testStruct.x,testStruct.y);
return EXIT_SUCCESS;
}
The problem is that I get error incompatible types when assigning to type 'testStructType' from 'void*'
What am I doing wrong? Can somebody please help me?
testStruct=pwill fail, becausetestStructis a structure, andpis a generic pointer. Actually, you probably don't need to use generic pointers (i.e.void *) at all. Just use pointers to the structure, and try to use that as much as you can. Many socket receive/send functions wantschar *for data, but then just cast the pointer to the structure when needed. If a function expects a generic pointer, then no casting is needed at all. - Some programmer dudetestStruct=p;: Different type as(testStructType) = (void*), change totestStruct=*(testStructType *)p;- BLUEPIXY