C 通过'的参数1;printf';从整数生成指针
我一直在犯这个错误C 通过'的参数1;printf';从整数生成指针,c,arguments,C,Arguments,我一直在犯这个错误 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 argumen
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]
当我试图用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");
}
}
这里
将字符而不是格式字符串作为第一个参数传递给printf()
。应该是
printf("%c", c);
或者
putchar(c);
我知道这是一年后的事了,但请记住#可能会被shell用作内联注释 所以“/box2 5#”将argc设为1,argv设为仅包含一个位置的字符串数组:“5”
在#之后的任何内容都将在shell调用您的程序之前被丢弃。请修复您的缩进!另外,请仔细查看行
charc=argv[2]代码>好肉汁,为什么99%的SO代码示例没有缩进???如果缩进正确,他们更可能会注意到错误,不需要发布…这很有效,但现在在我编译并尝试运行它之后,我得到了分段错误(内核转储)@user3015970:这是因为代码中还有一些错误:main()
的声明是错误的,并且两者都是int n=argv[1]代码>和字符c=argv[2]代码>应给出编译器警告或错误。-也许您应该首先尝试修复所有警告。如果需要更多帮助,请更新问题。
printf("%c", c);
putchar(c);