Variable size two dimensional array in C -


so i've been trying store ppm file in program manipulated, successful stored colors, i've made progress on colors.

during asking question on stack overflow (for loop stops no reason) convinced method bit shoddy, don't understand reasoning using follow:

color (*colors)[width] = malloc( sizeof(color[height][width]) ); 

can break down line of code doing , explain type is. can store in struct , successful return it.

example: used pointers pointers, allocated height , each pointer allocated width. means each pointer create color, increment along width until im @ end, reset pointer , increment height , loop. after got full image return store in follow:

typedef struct {     char code[code_length];     comment *commentppm;     int width, height, max;     color **colorvalues; } ppm; 

using:

ppmfile->colorvalues = getcolors(fd, ppmfile->width, ppmfile->height); 

and

typedef struct{     int red, green, blue; } color;  color * getnextcolor(file *fd);  color **getcolors(file *fd, int width, int height){     printf("\nentered colors");     color **colors = malloc(sizeof(color*)*height);     printf("\nallocated %d space height",height);      int i,j;     for(i = 0; < height; i++, colors++){         *colors = malloc(sizeof(color)*width);         printf("\nallocated %d space width",width);         for(j = 0; j < width; j++, *(colors++)){             printf("\nlooping through colors point (%d,%d)", j,i);              //*colors = getnextcolor(fd);         }         *colors -= width;         printf("\nmoved pointer *colors %d spaces",width);     }      colors -= height;     printf("\nmoved pointer colors %d spaces",height);      return colors; } 

in order understand this, need first understand concepts:

  • array pointers (not confused pointer first element)
  • variable-length arrays, known vlas.

given know above is, formally proper way declare array pointer 2d vla:

color (*colors)[height][width]; 

and when call malloc, tell allocate enough space such array:

malloc( sizeof(color[height][width]) ) 

you end with

colors = malloc( sizeof(color[height][width]) ); 

however, since colors in example array pointer, have de-reference each time wish access array:

(*colors)[i][j] = something; 

this syntax not practical , hard read.

therefore, can use trick , skip inner-most dimension when declare array pointer. instead of pointer 2d array, skip inner-most dimension , declare array pointer 1d array:

color (*colors)[width] 

but use same malloc call before. because can take advantage of array pointer arithmetic:

colors[i][j] = something; 

which means, "in array-of-arrays, give me array number i, item number j".

it works same reason int* x = malloc(sizeof(int[n])); ... x[i] = something; works, of course gives int number i.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -