I can’t seem to understand the difference between the following to pointer notations, can someone please guide me?
typedef struct some_struct struct_name;
struct_name this;
char buf[50];
this = *((some_struct *)(buf));
Now I tried to play around a bit and did the above thing like:
struct some_struct * this;
char buf[50];
this=(struct some_struct *)buf;
As far as I am concerned I think both the implementations should generate the same result, Can someone guide me whether there is a difference between the two and if yes can some one point it out?
Thanks.
In your first snippet,
thisis not a pointer, it’s an instance ofsome_struct. The assignment you made did a shallow copy (i.e.memcpy()) of what’s in buf as if it were an instance ofsome_structas well.In the second snippet,
thisis a pointer, and it’s just pointed to the address ofbuf.So, basically to sum up, first snippet
thisis not a pointer and the struct is copied into it. In the second, it’s a pointer and assigned to the same memory asbuf(i.e. not a copy).