c - Passing pointer of pointers to function, returning both an int and an address (by parameter) -



c - Passing pointer of pointers to function, returning both an int and an address (by parameter) -

i've got function which, is, works correctly. rest of programme has limitation in i've preset size of array (the space allocated). obviously, problematic should event arise in need space array. want add together dynamic allocation of memory program.

but i'm having issue whole pointer pointer concept, , i've utterly failed find online explanation makes sense me... think i'll want utilize malloc(iread + 1) array of right size, i'm not sure should assigned to... *array? **array? i'm not @ sure.

and i'm not clear on while loops. &array[iread] no longer work, , i'm not sure how hold of elements in array when there's pointer pointer involved.

can point (heh pointer pun) me in right direction?

i can think of next approaches.

first approach

make 2 passes through file. in first pass, read numbers , discard them maintain counting number of items. allocate memory 1 time items.

rewind file , create sec pass through it. in sec pass, read , store numbers.

int getnumberofitems(file* fp, int hexi) { int numitems = 0; int number; char const* format = (hexi == 0) ? "%x" : "%d"; while (fscanf(fp, format, &number) > 0) { ++numitems; homecoming numitems; } void read(int *array, file* fp, int numitems, int hexi) { int = 0; char const* format = (hexi == 0) ? "%x" : "%d"; ( = 0; < numitems; ++i ) fscanf(fp, format, &array[i]); } int main(int argc, char** argv) { int hexi = 0; file* fp = fopen(argv[1], "r"); // if ( fp == null ) // add together error checking code // number of items in file. int numitems = getnumberofitems(fp, hexi); // allocate memory items. int* array = malloc(sizeof(int)*numitems); // rewind file before reading info frewind(fp); // read data. read(array, fp, numitems, hexi); // utilize info // ... // ... // dealloate memory free(array); }

second approach.

keep reading numbers file. every time read number, utilize realloc allocate space additional item.

store in reallocated memory.

int read(int **array, char* fpin, int hexi) { int number; int iread = 0; // local variable ease of use. int* arr = null; char const* format = (hexi == 0) ? "%x" : "%d"; file *fp = fopen(fpin, "r"); if (null == fp){ printf("file open error!\n"); exit(-1); } while (fscanf(fp, format, &number) > 0) { arr = realloc(arr, sizeof(int)*(iread+1)); arr[iread] = number; iread += 1; } fclose(fp); // homecoming array in output argument. *array = arr; homecoming iread; } int main(int argc, char** argv) { int hexi = 0; int* array = null; // read data. int numitems = read(&array, argv[1], hexi); // utilize info // ... // ... // dealloate memory free(array); }

c arrays pointers

Comments

Popular posts from this blog

php - Edges appear in image after resizing -

ios8 - iOS custom keyboard - preserve state between appearances -

Delphi change the assembly code of a running process -