0
#include <stdio.h>
#define MAX 3 // students in class 
#define LEN 20 // max lengths stydent's name

typedef struct {
    char name[LEN];
    int am;
    float tv;
}student;

void read (student board[]) {       //this function should fill the board of structs
    int i;
    for (i=0; i<MAX; i++) {
        printf("\n give student's name");
        scanf ("%s",&board[i].name);
    }

}

void read (student board[]);

int main (void) {
    student class[MAX];
    read (class);
return 0;   
} 

when i try to compile it i get this error

let2.c:15:3: warning: format ‘%s’ expects argument of type ‘char ’, but argument 2 has type ‘char ()[20]’ [-Wformat=]
let2.c:15:3: warning: format ‘%s’ expects argument of type ‘char ’, but argument 2 has type ‘char ()[20]’ [-Wformat=]

Paul Roub
  • 36,322
  • 27
  • 84
  • 93

1 Answers1

0

Lose the & in your scanf call. You want a pointer to the first character of the string, not a pointer to the array itself:

scanf("%s", board[i].name);

Or, equivalently:

scanf("%s", &board[i].name[0]);
Carl Norum
  • 219,201
  • 40
  • 422
  • 469
  • WOw !! So fast and accurate. Thank you very much !! not a pointer to the array itself. could you explain that a litle bit more? – noobgrammer Sep 19 '14 at 20:50
  • This would be a great opportunity to mention the dangers of `%s` without a maximum field width. – mafso Sep 19 '14 at 20:53
  • I have to say that this is kind of puzzling, since for any `Type arr[N]`, the values of `arr` and `&arr` are equal as long as you "test them" in the scope of declaration of `arr`. So either the compiler issues a futile warning here, or the values of `board[i].name` and `&board[i].name` are not identical since they are not used in the "original" scope of declaration. – barak manos Sep 19 '14 at 21:01