Passing argument 1 of 'printf' makes a pointer from an integer

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

#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 tried to figure this out, but still getting the same error. Any help would be greatly appreciated. The program is designed to print a field like this, indicating the quantity and nature that create it.

    ./box2 5 #
    #####
    #   #
    #   #
    #   #
    #   #
    #####
+4
source share
2 answers

Here

printf(c);

you pass a character instead of a format string as the first argument to printf(). It should be

printf("%c", c);

or alternatively

putchar(c);
+8
source

I know this a year later, but keep in mind that # can be used as an inline comment of your shell.

, "./box2 5 #" argc 1 argv , : "5".

, # , .

+1

Source: https://habr.com/ru/post/1532546/


All Articles