【发布时间】:2015-01-15 15:58:46
【问题描述】:
我有这个结构,一个包含学生姓名和分数的简单结构。当我尝试将用户输入读入名称(字符数组)时,我收到一条警告,指示以下内容:
format %s expects char *, but has char*[20]
我知道这是因为 char arrays 不能在 C 中赋值,所以必须使用 strcpy。 SO 上的这个question 有一个很好的理由。但是,如何修复程序中的警告?不要以为我可以在这里使用 strcpy。
#include <stdio.h>
typedef struct _student
{
char name[20];
unsigned int marks;
} student;
void read_list(student list[], int SIZE);
void print_list(student list[], int SIZE);
int main()
{
const int SIZE=3;
student list[SIZE];
//function to enter student info.
read_list(list, SIZE);
//function to print student info
print_list(list, SIZE);
return 0;
}
void read_list(student list[], int SIZE)
{
int i;
char nm[20];
for (i=0;i<SIZE;i++)
{
printf("\n Please enter name for student %d\n", i);
scanf("%s",&list[i].name);
printf("\n Please enter marks for student %d\n", i);
scanf("%u", &list[i].marks);
}
}
void print_list(student list[], int SIZE)
{
int i;
printf("\t STUDENT NAME STUDENT MARKS\t \n");
for(i=0;i<SIZE;i++)
{
printf("\t %s \t %u\n", list[i].name, list[i].marks);
}
}
程序确实给出了正确的输出,但警告仍然存在。
【问题讨论】:
-
在此处删除
&scanf("%s",&list[i].name); -
程序确实给出了正确的输出,因为对于静态分配的数组
arr,arr和&arr的值是相同的。