所以我试图制作一个程序来读取ppm文件并将其存储在内存中,我已经完成了一切颜色,这个功能给我带来了问题:
typedef struct{
int red, green, blue;
} COLOR;
COLOR * getNextColor(FILE *fd);
COLOR **getColors(FILE *fd, int width, int height){
printf("\nentered get colors");
COLOR **colors = malloc(sizeof(COLOR*)*height);
printf("\nallocated %d space height",height);
int i,j;
for(i = 0; i < height; i++, colors++){
*colors = malloc(sizeof(COLOR)*width);
printf("\nallocated %d space width",width);
for(j = 0; j < width; j++, *colors++){
printf("\nlooping through to get the colors for point (%d,%d)", j,i);
//*colors = getNextColor(fd);
}
*colors -= width;
printf("\nmoved the pointer for *colors back %d spaces",width);
}
colors -= height;
printf("\nmoved the pointer for colors back %d spaces",height);
return colors;
}
我传入的文件指针当前指向第一种颜色的第一个数字,宽度= 400,高度为530.输出如下所示:
allocated 530 space height
allocated 400 space width
looping through to get the colors for point (0,0)
looping through to get the colors for point (1,0)
looping through to get the colors for point (2,0)
...
looping through to get the colors for point (398,0)
looping through to get the colors for point (399,0)
moved the pointer for *colors back 400 spaces
allocated 400 space width
looping through to get the colors for point (0,1)
looping through to get the colors for point (1,1)
...
looping through to get the colors for point (398,1)
looping through to get the colors for point (399,1)
moved the pointer for *colors back 400 spaces
allocated 400 space width
和模式一直重复到
looping through to get the colors for point (399,36)
然后崩溃了。有什么想法吗?
答案 0 :(得分:3)
*colors++
存在问题,这可能不代表您的想法。这是由于运算符优先级,最高优先级有后缀增量/减量运算符,低优先级有间接性。所以*colors++
实际上意味着*(colors)++
没有多大意义。你可能意味着(*colors)++