Add struct to struct array using void pointer to said array in C -
let's have following struct , array of struct:
struct fileinfo { int ascii[128]; //space store counts each ascii character. int lnlen; //the longest line’s length int lnno; //the longest line’s line number. char* filename; //the file corresponding struct. }; struct analysis fileinfo_space[8]; //space info 8 files
i want have function add new struct array. must take void pointer position store struct argument
int addentry(void* storagespace){ *(struct fileinfo *)res = ??? //cast pointer struct pointer , put empty struct there (struct fileinfo *)res->lnlen = 1; //change lnlen value of struct 1 }
my questions are:
what goes in place of ??? tried
(fileinfo){null,0,0,null}
per this stackoverflow response. `error: ‘fileinfo’ undeclared (first use in function)how create void pointer array? (void *)fileinfo_space correct?
i required use void *
argument function assignment. it's not me.
let's have memory block passed storagespace
void pointer:
you have define constant able initialize (unless you're using c++11), let's call init
. btw assignment value wrong: first member array of int. cannot pass null
it. zero-fill show below.
then cast void pointer pointer on struct, initialize copying init struct, modify @ will...
int addentry(void* storagespace){ static const struct fileinfo init = {{0},0,0,null}; struct fileinfo *fi = (struct fileinfo *)storagespace; *fi = init; //cast pointer struct pointer , put empty struct there fi->lnlen = 1; //change lnlen value of struct 1 }
Comments
Post a Comment