我们有一个声明了这些结构的头文件:
typedef struct{
unsigned short rgb[3];
}PIXEL_T;
typedef struct{
int format;
int nrows;
int ncolumns;
int max_color;
PIXEL_T **pixels;
}PBM_T;
我们正在尝试访问 rgb[0] 字段以向其写入一个数字。但是由于我们是新手,使用“指针的指针”数组证明是困难的。这是我们最好的错误尝试:
/*pbm was previously declared as a PBM_T structure. rows and columns are auxiliary variables to send to the nrows and ncolumns field. we're suppose to create a bitmap matrix*/
pbm->(**pixels) = malloc(sizeof(int *)*rows);
if (pbm->(**pixels) == NULL)
ERROR(ERR_ALLOC,"Error allocating memory for the bitmap matrix");
int i;
for(i = 0; i < columns; i++) {
pbm->pixels[i] = malloc(sizeof(int)*columns);
}
pbm->&nrows = rows;
pbm->&ncolumns = columns;
while((getline(&line, &len, file_stream)) != 1) {
getline(&line, &len, file_stream);
sscanf(line,"%d",&pbm->pixels[i][j]->rgb[0]); /* i and j are for two for cycles we're going to implement */
}
基本上,我们最大的问题是访问该字段的正确方法。所有的 * 和 & 都让我们很困惑。如果有人也可以简要解释它的工作原理,我们将不胜感激。先感谢您。