简体   繁体   English

C-“警告:格式”%c”期望参数类型为“ char *”,但是参数3的类型类型为“ int”:

[英]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.

相关问题 警告:格式&#39;%c&#39;需要类型&#39;int&#39;,但参数2的类型为&#39;char *&#39; - warning: format '%c' expects type 'int', but argument 2 has type 'char *' GCC编译器警告:格式&#39;%c&#39;需要类型为&#39;char *&#39;的参数,但参数2为类型&#39;int *&#39;[-Wformat] - GCC compiler warning: format ‘%c’ expects argument of type ‘char *’, but argument 2 has type ‘int *’ [-Wformat] 警告:格式“c”需要“int”类型的参数,但参数 2 的类型为“char” - warning :format 'c' expects argument of type 'int', but argument 2 has type 'char ' 警告:格式&#39;%c&#39;需要&#39;char *&#39;类型的参数,但参数3的类型为&#39;int&#39; - warning: format '%c' expects argument of type 'char *', but argument 3 has type 'int' 格式 %c 需要 char* 类型的参数,但具有 int - Format %c expects argument of type char* but has int 警告:格式&#39;%s&#39;需要类型&#39;char *&#39;,但参数2的类型为&#39;int&#39; - warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int’ 警告:格式%s期望为char *类型,但参数2为int类型 - warning: format %s expects type char * but argument 2 has type int 格式 &#39;%c&#39; 需要类型为 &#39;char *&#39; 的参数,但参数 2 的类型为 &#39;char (*)[0]&#39; - format ‘%c’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[0]’ 在 C 中,如何修复此警告:格式“%s”需要“char *”类型的参数,但参数 3 的类型为“char (*)[100] - In C, how to fix this warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘char (*)[100] C 语言:收到错误:警告:格式 &#39;%d&#39; 需要类型为 &#39;int *&#39; 的参数,但参数 2 的类型为 &#39;int **&#39; [-Wformat=] - C language: received error: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int **’ [-Wformat=]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM