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