【发布时间】:2013-11-02 18:28:53
【问题描述】:
我们有一个声明了这些结构的头文件:
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 */
}
基本上,我们最大的问题是访问该字段的正确方法。所有的 * 和 & 都让我们很困惑。如果有人也可以简要解释它的工作原理,我们将不胜感激。提前谢谢你。
【问题讨论】:
-
@关于他们的工作方式在这里阅读我的回答stackoverflow.com/questions/19602300/… 可能会有所帮助
-
感谢您的反馈。我们得到了 & 和 * 运算符的含义,当我们假设使用它们来访问结构字段时,它会变得令人困惑,尤其是那些 '**' 。
-
陛下,
*和&运算符以及括号不能应用于字段名称,例如pbm->&nrows或pbm->(**pixels)。请将它们排除在外,您的受试者会感谢您的。
标签: c arrays pointers structure