Pointer to pointer in C -
i hope can me.
i have function in c, takes file, reads line line , stores every line string. works in function
int createdownloadlist(file **dllistref, dltask* tasklist) { ... tasklist = (dltask*) malloc(tasksize*allocsize); int num = 0; while(getline(&line, &linesize, *dllistref) > 0) { ... tasklist[num] = task; num++; if(num%8 == 0) { tasklist = realloc(tasklist, (num+allocsize)*tasksize); } } return num; }
but want access pointer tasklist outside of function. tried change
int createdownloadlist(file **dllistref, dltask** tasklist) { size_t linesize = 256; char* line = (char*) malloc(linesize); size_t tasksize = sizeof(dltask); int allocsize = 8; *tasklist = (dltask*) malloc(tasksize*allocsize); int num = 0; while(getline(&line, &linesize, *dllistref) > 0) { ... *tasklist[num] = task; num++; if(num%8 == 0) { *tasklist = realloc(tasklist, (num+allocsize)*tasksize); } } return num; }
but segmentation fault after third task , don't know why. hope can me, clueless, why won't work. oh, , that's how call second function in main method:
dltask* tasklist = null; numoftasks = createdownloadlist(&fileref_dllist, &tasklist)
i added "&" in call, otherwise it's same call first function.
line
*tasklist = realloc(tasklist, (num+allocsize)*tasksize);
have be
*tasklist = realloc(*tasklist, (num+allocsize)*tasksize);
edit
the second error, found out @user3121023, is:
*tasklist[num] = task;
that should be
(*tasklist)[num] = task;
Comments
Post a Comment