-1
#include<stdio.h>
int main()
{
  char str[6][50] ;    // a 2d character array
  int  i ;
  for(i = 0 ; i < 6 ; i++)
  {
    scanf("%s",(str+i)) ; // in here the warning was shown
  }
  return 0 ;
}`

During Output :- scanf() is showing warning on compilation - warning: format ‘%s’ expects argument of type ‘char ’, but argument 2 has type ‘char ()[50]’ [-Wformat=]

slothfulwave612
  • 1,349
  • 9
  • 22
  • 1
    As per the comment, str is a 2d array. The scanf function expects a 1d array but instead gets this. What is your question? – matt_rule Nov 23 '17 at 14:23
  • 4
    What's not clear about warning? Use `scanf("%s",*(str+i));` or `scanf("%s",str[i]);`. – P.P Nov 23 '17 at 14:24
  • I bet the problem is that scanf expects type `char*` but that the argument is of type `char(*)[50]`. – Lundin Nov 23 '17 at 14:53

2 Answers2

3

Remember that arrays naturally decays to pointers to their first element? That means str by itself is treated as a pointer. With str + i you are doing pointer arithmetic, the result of which is another pointer (to the i:th element in this case). The type of that pointer is a pointer to an array, char (*)[50], it's not the array itself.

What you need to do is to dereference the pointer: *(str + i). Or as it's the same, str[i].

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

It seems like you are trying to access each element of str in turn. To do this use scanf("%s",str[i]); (as per usr's comment).

matt_rule
  • 1,220
  • 1
  • 12
  • 23