简体   繁体   English

警告:格式'%c'需要类型'int',但参数2的类型为'char *'

[英]warning: format '%c' expects type 'int', but argument 2 has type 'char *'

I'm trying to print all characters stored in hex array to the screen, one by one, but I get this strange error in line 16. As far as I know, %c should be expecting a char, not an int. 我正在尝试将以十六进制数组存储的所有字符逐个打印到屏幕上,但我在第16行中得到了这个奇怪的错误。据我所知,%c应该期待一个字符,而不是一个int。 Why I'm getting this error? 为什么我收到此错误? Below is my code, thanks. 下面是我的代码,谢谢。

    #include <stdio.h>
    #include <stdlib.h>
    #include <limits.h>
    #include <ctype.h>
    #include <string.h>

    int main() 
    {
        char hex[8] = "cf0a441f";
        int hexCounter;
        char *currentHex;

        for(hexCounter=0; hexCounter<strlen(hex); hexCounter++)
        {
            currentHex = &hex[hexCounter];
            printf("%c",currentHex);
        }   
         return 0;
    }

You mean 你的意思是

printf("%c", *currentHex);

In my opinion you can remove the entire idea of currentHex since it just adds complexity for no value. 在我看来,你可以删除currentHex的整个想法,因为它只是增加了没有价值的复杂性。 Simply do: 简单地说:

printf("%c", hex[hexCounter]);

The important point is that you're supposed to pass the value of the character itself, not it's address which is what you're doing. 重要的一点是,你应该传递角色本身的价值 ,而不是你正在做的地址。

You have hex[hexCounter] as a char so when you set 你有hex[hexCounter]作为char所以当你设置

currentHex = &hex[hexCounter];

you are setting currentHex to the address of a char , ie a char * . 您将currentHex设置为char的地址,即char * As such, in your printf you need 因此,在您的printf您需要

printf("%c",*currentHex);

What you are doing is unnecessary anyway, since you could just do 无论如何,你所做的事情是不必要的,因为你可以做到

printf("%c",hex[hexCounter]);

currentHex should be of type char , not char * . currentHex应该是char类型,而不是char *

 char currentHex;

 [..]

 currentHex = hex[hexCounter];
 printf("%c",currentHex);

If you really want it to be a pointer, dereference it to print: 如果你真的希望它是一个指针,请取消引用它来打印:

printf("%c",*currentHex);

Here's the modified code which runs fine for me - 这是修改后的代码,对我运行正常 -

#include <stdio.h>
#include <stdlib.h>

#include <limits.h>
#include <ctype.h>
#include <string.h>

int main() 
{
    char hex[9] = "cf0a441f";
    unsigned int hexCounter; 
    char *currentHex;
    for(hexCounter=0; hexCounter<strlen(hex); hexCounter++)
    {
        currentHex = &hex[hexCounter];
        printf("%c",*currentHex);
    }   
     return 0;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 警告:格式&#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 C-“警告:格式”%c”期望参数类型为“ char *”,但是参数3的类型类型为“ int”: - C - “warning: format ”%c“ expects argument of type ”char *“, but argument 3 has type ”int":? 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 警告:格式“%s”需要类型“char *”,但参数 2 的类型为“char (*)” - warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)’ 警告:格式“%d”需要“int *”类型的参数,但参数 3 的类型为“uint8_t * {aka unsigned char *} - warning: format ‘%d’ expects argument of type ‘int *’, but argument 3 has type ‘uint8_t * {aka unsigned char *} 警告:格式&#39;%s&#39;期望参数类型为&#39;char *&#39;,但是参数2使用argv类型为&#39;int&#39; - warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ using argv
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM