What is the meaning of this statement in C?
struct foo *ptr = (struct foo *) p2;
What is the meaning of this statement in C?
struct foo *ptr = (struct foo *) p2;
You're not giving all the informations we need:
struct name *ptr = (Same struct name *) p2;
let's make it something that can compile:
struct foo* ptr = (struct foo*) p2;
but now we're missing what p2 is. So, I'm going to assume that p2 is a plain C pointer, i.e. void*:
void* p2;
struct foo* ptr = (struct foo*) p2;
So here you're assigning to ptr the address pointed by p2. In my example now, this is pretty pointless… but if you allocate some memory:
void* p2 = malloc(sizeof(struct foo));
struct foo* ptr = (struct foo*) p2;
then p2 is having the address of a memory space that then you assign to ptr.
The example I'm giving you here is using two variables for doing the usual:
struct foo* ptr = (struct foo*) malloc(sizeof(struct foo));
Starting at the left hand side:
struct foo *ptr
declares ptr to be of type struct foo *, that is a pointer to struct foo.
The = initializes that ptr variable to whatever the right hand side evaluates as.
And the right hand side
(struct foo *) p2
is an expression that casts p2 to be of type struct foo *.