gpt4 book ai didi

c - 结构体和字符串

转载 作者:行者123 更新时间:2023-11-30 17:28:47 24 4
gpt4 key购买 nike

#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: 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=]

最佳答案

丢失 scanf 调用中的 &。您需要一个指向字符串第一个字符的指针,而不是指向数组本身的指针:

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

或者,等价:

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

关于c - 结构体和字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25942086/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com