2
#include<stdio.h>

struct employee
{
  char name[25] ;
  int id ;
  int age ;
  float inc ;
} b[3] ;

int main()
{
  int i ;
  for(i = 0 ; i < 3 ; i++)
  {
    printf("Enter name , id , age , inc of employee %d :- ",i+1) ;
    scanf("%s%d%d%f",&b[i].name,&b[i].id,&b[i].age,&b[i].inc) ;
  }
  printf("\n") ;
  for(i = 0 ; i < 3 ; i++)
  {
    printf("%s%d%d%f\n",b[i].name,b[i].id,b[i].age,b[i].inc) ;
  }
  return 0 ;
}

Output on compiling :

warning: format ‘%s’ expects argument of type ‘char ’, but argument has type ‘char ()[25]’ [-Wformat=] scanf("%s%d%d%f",&b[i].name,&b[i].id,&b[i].age,&b[i].inc) ;

How to fix the warning ?

Ron
  • 14,674
  • 4
  • 34
  • 47
slothfulwave612
  • 1,349
  • 9
  • 22
  • 1
    No need for `&` on `b[i].name` – KevinDTimm Nov 28 '17 at 20:14
  • 2
    An array name, like `b[i].name`, will [decay to a pointer](https://stackoverflow.com/questions/1461432/what-is-array-decaying). No need for the `&`. – Bo Persson Nov 28 '17 at 20:15
  • @KevinDTimm In `scanf` you do. – Henri Menke Nov 28 '17 at 20:15
  • 1
    @HenriMenke - no, you do not – KevinDTimm Nov 28 '17 at 20:15
  • Have you tried referring to the C reference? There is even a working example: http://www.cplusplus.com/reference/cstdio/scanf/ – Nick Nov 28 '17 at 20:17
  • @Nick - does the same exist for the `c` language? – KevinDTimm Nov 28 '17 at 20:19
  • Plus, your scanf may not store what you think it will store w.r.t. whitespace or lack thereof, etc. – Nick Nov 28 '17 at 20:19
  • @KevinDTimm - The C++ C library is largely the same as ISO1990 C; exactly the same for the purposes of this question. I just linked the first source I knew was reputable. – Nick Nov 28 '17 at 20:22
  • @Nick - as these are 2 separate languages, I would expect that there should be a page for `c` – KevinDTimm Nov 28 '17 at 20:23
  • Fine: https://www-s.acm.illinois.edu/webmonkeys/book/c_guide/2.12.html#scanf -- but now my eyes hurt. – Nick Nov 28 '17 at 20:24
  • @KevinDTimm Yes, http://en.cppreference.com/w/c/io/fscanf or http://www.manpagez.com/man/3/scanf/ for example. – Bob__ Nov 28 '17 at 20:34
  • @Bob__ : nope (C++ reference C++98, C++03, C++11, C++14, C++17, C++20) – KevinDTimm Nov 28 '17 at 20:49
  • @KevinDTimm Do you mind elaborate? I specifically linked to the C documentation page (see the references throughout the whole page and at the bottom in particular) even if that site is well known as C++ reference. – Bob__ Nov 28 '17 at 20:56

2 Answers2

4

&b[i].name is a pointer to an array of char (it's a char (*)[25]), but scanf wants a pointer to a char (a char*).

You need to pass a pointer to the array's first element.

You can either get that explicitly with &b[i].name[0], or rely on implicit conversion with just b[i].name

molbdnilo
  • 64,751
  • 3
  • 43
  • 82
2

No need for & on b[i].name as this decomposes to a pointer to the zeroth element already

KevinDTimm
  • 14,226
  • 3
  • 42
  • 60