0

I've just started learning how to code in C for uni. I'm trying to take a step up from char arrays and trying to store strings in char pointers, but for some reason the terminal is giving me a weird error.

I'm using CLion and the Ubuntu Terminal. When I run the command make main, it creates a make in the directory, but returns a warning:

main.c:6:19: warning: format ‘%s’ expects a matching ‘char *’ argument 
[-Wformat=]
scanf("%[^\n]%s", s);
             ~^

Here is my code:

#include<stdio.h>

int main(void)
{
    char *s;
    scanf("%[^\n]%s", s);
    printf("Hello World! %s\n", s);
}

And then when I run ./main, the program outputs: Hello World! (null). I'm literally passing a char* into scanf so I don't really know what is causing this error. As a matter of fact, when I tried replacing %s with %*c, make tells me that *c expects an int! I'm honestly pretty confused about his whole thing, so any help would be quite appreciated.

dkapur17
  • 476
  • 1
  • 3
  • 11

0 Answers0