如何将 char* argv[1] 转换为 int 并在 C 中不发出警告地打印它
How to convert char* argv[1] to int and print it without warning in C
我想将 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);
}
您正在传递 key_length
的地址,而错误指出,printf
只需要该值。试试这个:
printf("key_length = %d", key_length);
请参阅 this tutorial C 格式说明符。
printf("%d")
期望的类型是 int。
你给的是一个指向 int 的指针。
所以你应该改变
printf("key_length = %d", &key_length);
到
printf("key_length = %d", key_length);
我想将 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);
}
您正在传递 key_length
的地址,而错误指出,printf
只需要该值。试试这个:
printf("key_length = %d", key_length);
请参阅 this tutorial C 格式说明符。
printf("%d")
期望的类型是 int。
你给的是一个指向 int 的指针。
所以你应该改变
printf("key_length = %d", &key_length);
到
printf("key_length = %d", key_length);