c - Typecasting structure pointers -
hie guys. studying structures , pointers using book called pointers in c: hands on approach
, on page 107, came across incomplete example of struct type casting. tried make work implementing function receivedata()
,adding headers , making few changes.
complete code:
#include <stdio.h> #include <string.h> #include <stdlib.h> struct signature { char sign; char version; }; struct id { char id; char platform; }; struct data { struct id idv; struct signature sig; char data[100]; }; static void receivedata(struct data *d); static struct signature *extractsignature(struct data *d); static struct id *extractid(struct data *d); int main(int argc, char *argv[]) { /* actual line in book : * struct data *img; no memory allocation or assignment null. * had errors allocated memory on heap before passing value * receivedata(); */ struct data *img = malloc(sizeof(struct data)); receivedata(img); /* actual line in book : * struct id *idval = extractid(&img); * struct signature *sig = extractsignature(&img); * erroneous because &img pointer * pointer (struct data**) not data type * extract functions' argument */ struct id *idval = extractid(img); struct signature *sig = extractsignature(img); printf("for signature:\n"); printf("sign = %c", sig->sign); printf(" version = %c\n\n", sig->version); printf("for id:\n"); printf("id = %c", idval->id); printf(" platform = %c", idval->platform); printf("\ndata = %s", img->data); return 0; } static struct signature *extractsignature(struct data *d) { struct signature *sig = (struct signature *)d; return sig; } static struct id *extractid(struct data *d) { struct id *idv = (struct id *)d; return idv; } static void receivedata(struct data *d) { struct data *dptr = d; char *ch = "coding cool!"; dptr->sig.sign = 's'; dptr->sig.version = '1'; dptr->idv.id = 'i'; dptr->idv.platform = 'p'; strncpy(dptr->data, ch, strlen(ch)); return; }
what understand casting inside function extractid()
, extraxtsignature()
because seems casting struct data *
retrieve individual members. ran program , value of first member of struct data
both sig member , id members. here output when struct id
comes first in struct data
:
for signature: sign = version = p id: id = platform = p data = coding cool!
and when struct signature
first element:
for signature: sign = s version = 1 id: id = s platform = 1 data = coding cool!
and finally, when char data[100]
first element:
for signature: sign = c version = o id: id = c platform = o data = coding cool!
may please explain type of casting, when use , best practices. thank much.
struct data { struct id idv; struct signature sig; char data[100]; };
for above definition, extractid
function below valid , right results.
static struct id *extractid(struct data *d) { struct id *idv = (struct id *)d; return idv; }
this because, struct id
first member of struct data
. so, when have struct data *d
, d
points starting address of struct data
, same starting address of first member struct id
.
so, if casting first member of struct, right per code have provided above.
Comments
Post a Comment