1

Beginner question. I have the following code:

char input[10];   
scanf("%s", &input);

My compiler throws the following warning:

warning: format '%s' expects type 'char *', but argument 2 has type 'char (*)[10]'
warning: format '%s' expects type 'char *', but argument 2 has type 'char (*)[10]'

Changing to:

char * input;

Does not seem to help. What am I doing wrong?

user207421
  • 305,947
  • 44
  • 307
  • 483
Hoa
  • 19,858
  • 28
  • 78
  • 107
  • the token `input` is already a `char *`, you are passing a `char **` – fbstj May 23 '13 at 07:12
  • 1
    Used in this context, the char array "decays" into a pointer to it's first element, so to a pointer to the stack allocated char array. – 0xCAFEBABE May 23 '13 at 07:16
  • 3
    Don't do this. You'll run into buffer overflows quickly. Use `scanf("%9s", input);`. –  May 23 '13 at 07:21
  • Also read [Smashing the stack for fun and profit](http://inst.eecs.berkeley.edu/~cs161/fa08/papers/stack_smashing.pdf). – devnull May 23 '13 at 08:16

6 Answers6

10

Because an array already can be used as a pointer, so you don't need to the address-of operator.

If you read the warning message again, you will see that when you use the address-of operator on the array, you get a pointer to the array.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
5

Try,

char input[10];   
scanf("%s", input);

You don't have to give the address-of operator (&) with the name of the array input.

Deepu
  • 7,592
  • 4
  • 25
  • 47
1

%s format string accepts argument of type char* where as &intput is of type char (*)[10] that is the reason you are getting warning.

format '%s' expects type 'char *', but argument 2 has type 'char (*)[10]

Note argument 2 is &input in scanf()

scanf("%s", &input);
       ^       ^ 
       |       argument-2 
       argument-1

To correct this code you should write scanf like:

scanf("%s", input);

Side note:: value wise both &input and input are same if you string address but semantically both are diffrent.

&input is address of array that is char(*) [10] type whereas input type is char[10]

To understand difference between &input and input read this two answers:

  1. What does sizeof(&arr) returns? , and
  2. Issue with pointer to character array C++

Edit:

Changing to: char *input will not help you instead it becomes undefined behavoiur unless and until you allocated memory to input

Community
  • 1
  • 1
Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
0

char input[10] is a character array.
input itself represents the base address of the array, i.e a char *.
You do not need to use the address of operator (&)with it. instead, use:

char input[10];   
scanf("%s", input);

If you use char * input;, you need to allocate space for it using malloc. But in this case also, no need to use the address of operator.

Dipto
  • 2,720
  • 2
  • 22
  • 42
0
scanf("%s", input);

you will get the answer.

0
char input[10];   
scanf("%s", &input);

scanf requires the address to store the data that will be recieved. Array names already act like pointers, So you don't need the & operator to obtain the address.

format '%s' expects type 'char *', but argument 2 has type 'char (*)[10]'

When you do &input it indicates a pointer that is pointing to a char array of 10 char. char (*)[10]

input is itself char* and is only necessary to be passed as 2nd argument to scanf.

Go through this too for arrays and pointers.

Suvarna Pattayil
  • 5,136
  • 5
  • 32
  • 59