How to initialize a structure pointer with another structure pointer inside by C? -
i try initialize structure pointer structure pointer inside c programming, otherwise segmentation fault. structure defined below:
`struct gfcontext_t{ char *filecontent; size_t filelength; char *response; int socket_hd; }; struct gfserver_t{ char *servername; int serverport; int maxconnection; ssize_t (*handler)(struct gfcontext_t *, char *, void * ); struct gfcontext_t *ctx; int status; };
the initialization give inside function:
gfserver_t * gfserver_create(){ struct gfserver_t *gfs; gfs=(gfserver_t*) malloc(sizeof(struct gfserver_t)); ......//how initialization? return gfs; }`
use:
gfs->ctx = malloc(sizeof(struct gfcontext_t));
or if want initialize gfcontext_t members null
gfs->ctx = calloc(1, sizeof(struct gfcontext_t));
Comments
Post a Comment