【发布时间】:2017-07-01 12:15:46
【问题描述】:
我在填充结构中的二维数组时遇到问题。它允许我为像素[0][1]、像素[0][2]....等做到这一点。但是,一旦我增加第一个索引,即像素 [1] [1],我会收到总线错误 10。任何帮助将不胜感激。我已经删除了一些代码以使其更具可读性,但如果您需要所有这些,请告诉我!该程序正在做的是读取 PPM 图像并显示信息,我遇到的问题是存储图像的像素。
#define MAX_HEIGHT 4
#define MAX_WIDTH 4
typedef struct Pixel{
int red;
int green;
int blue;
}Pixel;
typedef struct PPM{
char code[2];
int width, height;
int max;
Pixel * pixels[MAX_HEIGHT][MAX_WIDTH];
}PPM;
struct PPM * getPPM(FILE * fd){
PPM * image = NULL;
image = malloc(sizeof(PPM));
//have got all the other PPM info here ask if needed
int i;
int j;
for(i = 0; i<MAX_HEIGHT-1; i++){
for(j = 0; j<MAX_WIDTH-1; j++){
// struct Pixel newPPM_Pixel;
if(fscanf(fd, "%d %d %d", &image->pixels[i][j]->red, &image->pixels[i][j]->green, &image->pixels[i][j]->blue) == 3){
//rgb_array[i][j] = newPPM_Pixel;
printf("/ %d / %d / %d", image->pixels[i][j]->red, image->pixels[i][j]->green, image->pixels[i][j]->blue);
}
}
}
return image;
}
【问题讨论】:
-
Pixel * pixels[MAX_HEIGHT][MAX_WIDTH];-->Pixel pixels[MAX_HEIGHT][MAX_WIDTH];或者先分配再使用。 -
哦,谢谢!解决了这个问题,非常感谢!
标签: c arrays multidimensional-array struct ppm