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

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -