passing argument 1 of 'printf 'makes pointer from integer
Asked Answered
S

3

7

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 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 #
    #####
    #   #
    #   #
    #   #
    #   #
    #####
Saxophone answered 19/3, 2014 at 20:16 Comment(3)
please fix your indentation! Also, take a closer look at the line char c = argv[2];Hydroquinone
good gravy, why are 99% of SO code examples not indented???Guendolen
If it was indented properly they'd be more likely to notice their error and not need to post...Fraze
M
15

Here

printf(c);

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

printf("%c", c);

or alternatively

putchar(c);
Medulla answered 19/3, 2014 at 20:24 Comment(2)
That worked but now after I compile it and try to run it I get Segmentation Fault (core dumped)Saxophone
@user3015970: That's because there are some more errors in your code: The declaration of main() is wrong, and both int n = argv[1]; and char c = argv[2]; should give compiler warnings or errors. - Perhaps you should try to fix all warnings first. If you need more help, update the question.Medulla
T
3

The same warning will occur if you use

printf('someString: %s\n')

with single quotes as opposed to

printf("someString: %s\n")

Putting this here for reference, as it doesn't answer this specific question directly.

Tessera answered 6/6, 2022 at 16:17 Comment(0)
O
1

I know it's a year later, but keep in mind that # may be used as inline comment by your shell.

So "./box2 5 #" would have argc as 1 and argv as a string array containg only one position: "5".

Anything after # would be discarded before the shell called your program.

Octachord answered 24/3, 2015 at 18:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.