如何将char * argv [1]转换为int并在C中重复打印而不发出警告

问题描述 投票:0回答:2

我想将argv [1]转换为int。但我得到这个警告:

xorcipher.c:7:9: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat=]

而printf显示我

-799362156

如果我打字

./xorcipher 4

怎么纠正这个?

这是我的代码:

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

int main(int argc,char* argv[])
{
    int key_length = atoi(argv[1]);
    printf("key_length = %d", &key_length);
    return(0);
}
c char int warnings argv
2个回答
0
投票

您正在传递key_length的地址,其中错误表明,printf只需要该值。试试这个:

printf("key_length = %d", key_length);

请参阅C格式说明符上的this tutorial


0
投票

printf("%d")预期的类型是int。 你给的是指向int的指针。

所以你应该改变

printf("key_length = %d", &key_length);

printf("key_length = %d", key_length);
© www.soinside.com 2019 - 2024. All rights reserved.