传递'printf'的参数1使得指针来自整数

use*_*970 4 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 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]
Run Code Online (Sandbox Code Playgroud)

当我尝试用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");
    }
}
Run Code Online (Sandbox Code Playgroud)

我一直试图弄清楚,但一直得到同样的错误.任何帮助将不胜感激.该程序旨在打印出一个这样的框,给出了多少和制作它的角色.

    ./box2 5 #
    #####
    #   #
    #   #
    #   #
    #   #
    #####
Run Code Online (Sandbox Code Playgroud)

Mar*_*n R 8

这里

printf(c);
Run Code Online (Sandbox Code Playgroud)

你将字符而不是格式字符串作为第一个参数传递给printf().它应该是

printf("%c", c);
Run Code Online (Sandbox Code Playgroud)

或者

putchar(c);
Run Code Online (Sandbox Code Playgroud)

  • @user3015970:那是因为您的代码中还有更多错误:`main()`的声明是错误的,`int n = argv[1];`和`char c = argv[2];`都应该给出编译器警告或错误。- 也许您应该首先尝试修复*所有*警告。如果您需要更多帮助,请更新问题。 (2认同)