[英]C - “warning: format ”%c“ expects argument of type ”char *“, but argument 3 has type ”int":?
I am writing code to read a PPM file into a struct pixel_type array containing 3 unsigned chars r,g, and b. 我正在编写代码以将PPM文件读入包含3个无符号字符r,g和b的struct pixel_type数组。 The code that causes problems looks like this:
导致问题的代码如下所示:
struct pixel_type pArray[width][height], *pPtr[width][height];
pPtr[width][height] = &pArray[width][height];
for(h = 0; h < height; h++)
{
for(w = 0; w < width; w++)
{
fscanf(in, "%c%c%c", pPtr[w][h]->r, pPtr[w][h]->g, pPtr[w][h]->b);
}
}
When compiling, I get this message for all 3 "%c"s: 编译时,我收到所有3个“%c”的消息:
warning: 警告:
format â%câ expects argument of type âchar *â, but argument (3,4, or 5) has type âintâ [-Wformat]
What would be the best way to read the pixel values into the struct pixel_type array? 将像素值读入struct pixel_type数组的最佳方法是什么?
struct pixel_type
{
unsigned char r;
unsigned char g;
unsigned char b;
};
You only need one temporary pointer, not a whole array of them. 您只需要一个临时指针,而不是它们的整个数组。
struct pixel_type {
unsigned char r;
unsigned char g;
unsigned char b;
};
main()
{
struct pixel_type pArray[10][10], *pPtr;
int height = 10, width = 10, h, w;
char buf[32];
/* testing */
strcpy(buf, "abc");
for(h = 0; h < height; h++) {
for (w = 0; w < width; w++) {
pPtr = &pArray[width][height];
/* testing */
sscanf(buf, "%c%c%c", &pPtr->r, &pPtr->g, &pPtr->b);
/* fscanf(in, "%c%c%c", &pPtr->r, &pPtr->g, &pPtr->b); */
}
}
}
You don't need to declare a pointer to the array *pPtr[width][height]
, since pArray[width][height]
is already an array and this: 您无需声明指向数组
*pPtr[width][height]
的指针,因为pArray[width][height]
已经是一个数组,并且这样:
pArray[0][0];
is equivalent to this: 等效于此:
*pArray;
In the same way that this: 以这种方式:
&pArray[0][0];
Is equivalent to this: 等效于此:
pArray;
So you just need to point the address of where your data is (inside of the structure), after the pointer or the array had already been deferenced. 因此,仅在指针或数组已被引用之后,只需指向数据所在的地址(在结构内部)即可。
struct pixel_type pArray[width][height];
for(h = 0; h < height; h++)
{
for(w = 0; w < width; w++)
{
fscanf(in, "%c%c%c", &(pArray[w][h].r), &(pArray[w][h].g), &(pArray[w][h].b));
}
}
将2D指针数组pPtr[width][height]
更改为指针pPtr
将解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.