c - For loop stops for no reason -


so i'm trying make program read ppm file , store in memory, i've got working colors, function giving me problems:

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; } 

i'm passing in file pointer pointing @ first digit of first color, width = 400 , height 530. output looks this:

allocated 530 space height allocated 400 space width looping through colors point (0,0) looping through colors point (1,0) looping through colors point (2,0) ... looping through colors point (398,0) looping through colors point (399,0) moved pointer *colors 400 spaces allocated 400 space width looping through colors point (0,1) looping through colors point (1,1) ... looping through colors point (398,1) looping through colors point (399,1) moved pointer *colors 400 spaces allocated 400 space width 

and pattern repeats way to

looping through colors point (399,36) 

then crashes. ideas?

there problem *colors++ not mean think does. due operator precedence, highest precedence has postfix increment/decrement operators , lower precedence has indirection. *colors++ meanst *(colors)++ doesn't make sense. meant (*colors)++


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 -