结构和字符串

时间:2014-09-19 20:41:16

标签: c string struct char

#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;   
} 

当我尝试编译它时,我收到此错误

  

let2.c:15:3:警告:格式'%s'需要类型为'char '的参数,但参数2的类型为'char()[20]'[ - Wformat = ]
  let2.c:15:3:警告:格式'%s'需要类型为'char '的参数,但参数2的类型为'char()[20]'[-Wformat =]

1 个答案:

答案 0 :(得分:0)

&来电中丢失scanf。你想要一个指向字符串第一个字符的指针,而不是指向数组本身的指针:

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

或等同地:

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