C语言 传递'printf'的参数1使指针来自整数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22517148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
passing argument 1 of 'printf 'makes pointer from integer
提问by user3015970
I keep getting this error
我不断收到此错误
box2.c: In function 'printchars':
box2.c:26:4: warning: passing argument 1 of 'printf' makes pointer from integer without a
cast [enabled by default]
/usr/include/stdio.h:363:12: note: expected 'const char * __restrict__' but argument is
of type 'char' box2.c:26:4: warning: format not a string literal and no format arguments [-Wformat-security]
box2.c:39:8: warning: passing argument 1 of 'printf' makes pointer from integer without a cast [enabled by default]
/usr/include/stdio.h:363:12: note: expected 'const char * __restrict__' but argument is of type 'char'
box2.c:39:8: warning: format not a string literal and no format arguments [-Wformat-
security]
When I try to compile this program with gcc
当我尝试用 gcc 编译这个程序时
#include <stdio.h>
void printchars(char c, int n);
int main( int argc, char*argv){
int n = argv[1];
char c = argv[2];
int nn = atoi(n);
printchars(c, nn);
return 0;
}
void printchars(char c, int n){
int x;
for (x = n + 2 ; x > 0; x--){
if (x != 1 && x != n){
printf(c);
int count = n;
while (count - 2 != 0){
printf(" ");
count--;
}
}
else{
int num = n;
while (num != 0){
printf(c);
num--;
}
}
printf("\n");
}
}
I have been trying to figure it out, but keep getting the same error. Any help would be greatly appreciated. The program is meant to print out a box like this given how many and the character that makes it.
我一直在试图弄清楚,但不断收到同样的错误。任何帮助将不胜感激。该程序旨在打印出一个这样的盒子,给定数量和制作它的字符。
./box2 5 #
#####
# #
# #
# #
# #
#####
回答by Martin R
Here
这里
printf(c);
you pass the character instead of a format string as the first argument to printf(). It should be
您将字符而不是格式字符串作为第一个参数传递给printf(). 它应该是
printf("%c", c);
or alternatively
或者
putchar(c);
回答by Rafael Dantas
I know it's a year later, but keep in mind that # may be used as inline comment by your shell.
我知道这是一年后,但请记住,# 可能会被您的 shell 用作内联注释。
So "./box2 5 #" would have argc as 1 and argv as a string array containg only one position: "5".
因此,“./box2 5 #”会将 argc 设为 1,将 argv 作为只包含一个位置的字符串数组:“5”。
Anything after # would be discarded before the shell called your program.
# 之后的任何内容都将在 shell 调用您的程序之前被丢弃。

