1
我有一个结构内填充结构的二维数组的问题。它允许我为像素[0] [1],像素[0] [2] ....等等做。但是,一旦我增加第一个索引,即像素[1] [1],我得到一个总线错误10.任何帮助,将不胜感激。我已经删除了一些代码,使其更具可读性,但如果您需要所有这些代码,请告诉我!程序正在做的是读取PPM图像并显示信息,我遇到的麻烦是存储图像的像素。总线错误10与二维数组结构内部结构
#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;
}
'像素*像素[MAX_HEIGHT] [MAX_WIDTH];' - >'像素像素[MAX_HEIGHT] [MAX_WIDTH];'或在使用前分配它。 – BLUEPIXY
哦,谢谢!修复了非常感谢的问题! – fdbdcbc