1

I'm trying to read a string and print it in Linux using:

cc child.c -o child

#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[]) {  

    char st[100];

    scanf("%s", &st);
    printf("%s", st);

    return 0;
}

However I encounter this warning that will not allow me to compile.

child.c: In function ‘main’:
child.c:8:2: warning: format ‘%s’ expects argument of type ‘char ’, but argument 2 has type ‘char ()[100]’ [-Wformat=] scanf("%s", &st);

How can I solve it? Thanks!

2
  • Works fine for me! (Only think to change: scanf("%s", &st); to scanf("%s", st);) Commented Dec 17, 2014 at 22:41
  • Good that you had your compiler warnings enabled. Commented Dec 17, 2014 at 23:19

2 Answers 2

4

In C, when passed to a function, array names converted to pointer to the first element of array. No need to place & before st. Change

scanf("%s", &st);  

to

scanf("%s", st);
Sign up to request clarification or add additional context in comments.

Comments

2

In C array types degenerate to pointers. So when you take the address of an array, you actually get a pointer to a pointer. Just pass the array itself to scanf.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.