c - define pointer to pointer array -
i want use pointer pointer store dynamic array data set don't know how link them together. know how solve problem? how can initialize pointer pointer array using dynamic array ? , how can pick specific data set further program using pointer pointer?
float *data; float **dataindex; *dataindex = (float**)malloc(number * sizeof(float*)); data = (float*) malloc(size * sizeof(float)); for(i = 0; < size; i++){ scanf("%f", (data + i)); }
to dynamically allocate 2d array, need use loop initialize each pointer in array.
float **arr; size_t i, n; if ((arr = malloc(n * sizeof(float *)) == null) perror("malloc"); (i = 0; < n; ++i) if ((arr[i] = malloc(sizeof (float))) == null) perror("malloc");
don't forget free memory.
while (--n >= 0) free(arr[n]); free(arr);
you need careful free each subarray first, , free entire array.
Comments
Post a Comment