C 我是否以错误的方式使用结构?
我遇到了这个古怪而神秘(至少对我来说)的错误,我发现很难找到它。它在调用函数C 我是否以错误的方式使用结构?,c,struct,C,Struct,我遇到了这个古怪而神秘(至少对我来说)的错误,我发现很难找到它。它在调用函数input(student\u list1[MAX]、&total\u条目)的行中给出了一个错误其中编译器显示: “输入”中agument 1的类型不兼容 我做错了什么?我觉得它非常简单和愚蠢,但我已经通过代码多次现在没有任何用处 #define MAX 10 #define NAME_LEN 15 struct person { char name[NAME_LEN+1]; int age; }; void inp
input(student\u list1[MAX]、&total\u条目)的行中给出了一个错误代码>其中编译器显示:
“输入”中agument 1的类型不兼容
我做错了什么?我觉得它非常简单和愚蠢,但我已经通过代码多次现在没有任何用处
#define MAX 10
#define NAME_LEN 15
struct person {
char name[NAME_LEN+1];
int age;
};
void input(struct person student_list1[MAX], int *total_entries);
int main(void)
{
struct person student_list1[MAX];
int total_entries=0, i;
input(student_list1[MAX], &total_entries);
for(i=0; i<total_entries; i++)
{
printf("Student 1:\tNamn: %s.\tAge: %s.\n", student_list1[i].name, student_list1[i].age);
}
return 0;
} //main end
void input(struct person student_list1[MAX], int *total_entries)
{
int done=0;
while(done!=1)
{
int i=0;
printf("Name of student: ");
fgets(student_list1[i].name, strlen(student_list1[i].name), stdin);
student_list1[i].name[strlen(student_list1[i].name)-1]=0;
if(student_list1[i].name==0) {
done=1;
}
else {
printf("Age of student: ");
scanf("%d", student_list1[i].age);
*total_entries++;
i++;
}
}
}
#定义最大值10
#定义名称\u LEN 15
结构人{
字符名[name_LEN+1];
智力年龄;
};
无效输入(结构人员学生列表1[MAX],整数*总条目);
内部主(空)
{
结构人学生列表1[MAX];
int total_entries=0,i;
输入(学生列表1[最大值],&总输入);
函数参数中的for(i=0;istruct-person-student\u-list1[MAX]
实际上是指向struct-person-student\u-list1
的指针
student\u list1[MAX]
您传递的是数组struct person student\u list1[MAX]
的(越界)成员。有效数组索引应介于0
到MAX-1
之间
将其更改为:
input(student_list1, &total_entries);
请注意,此处数组名学生列表1
自动转换为指向学生列表1[0]
输入(学生列表1[MAX],&total\u条目);
应为输入(学生列表1,&total\u条目);
在C中
相等于
void input(struct person *student_list1, int *total_entries);
代码有很多错误;这是我试图使其更加健壮的尝试:
#include <stdio.h>
#include <string.h>
#define MAX 10
#define NAME_LEN 15
// use a typedef to simplify code
typedef struct person {
char name[NAME_LEN];
int age;
} person_t;
// size qualifier on student_list is redundent and person_t* does the same
void input(person_t *student_list, int *total_entries);
int main(void)
{
person_t student_list[MAX];
int total_entries, i;
// pass array and not the non-existent 'student_list[MAX]' element
input(student_list, &total_entries);
for(i=0; i<total_entries; i++)
{
// age is an int, not a string so use %d
printf("Student 1:\tName: %s.\tAge: %d.\n", student_list[i].name, student_list[i].age);
}
return 0;
} //main end
void input(person_t *student_list, int *total_entries)
{
int done = 0, i = 0;
*total_entries = 0;
while (i < MAX) {
printf("Name of student: ");
// use NAME_LEN instead of strlen(list[i].name) because latter is
// probably not initialized at this stage
if (fgets(student_list[i].name, NAME_LEN, stdin) == NULL) {
return;
}
// detect zero-length string
if (student_list[i].name[0] == '\n') {
return;
}
printf("Age of student: ");
scanf("%d", &student_list[i].age);
// read the newline
fgetc(stdin);
*total_entries = ++i;
}
}
#包括
#包括
#定义最大值10
#定义名称\u LEN 15
//使用typedef简化代码
typedef结构人{
字符名[name_LEN];
智力年龄;
}个人;;
//学生列表上的大小限定符是redundent,person\u t*也会这样做
无效输入(人员*学生列表,整数*总条目);
内部主(空)
{
个人/学生列表[最大值];
int total_条目,i;
//传递数组,而不是不存在的“student_list[MAX]”元素
输入(学生列表和总条目);
对于(i=0;i您不正确地使用数组。请检查将数组传递给函数的语法。typedef struct person{…}person\u t;
将提高可读性:)谢谢@Jack。我是编程新手,我确实看到了你的代码是如何更加健壮的。我是编程新手,有时在健壮的代码中思考会有困难。但是,在你使用fgets()时有一个问题
您使用NAME\u LEN
定义,该函数的参数是它可以读取的最大字符数,对吗?另外,不fgets()
读取的字符数比它读入字符串变量的字符数少一个吗?不学生列表[i].name
缺少“\0”字符?@ArmanIqbal它将始终为空,因此您可能希望将name\u LEN
常量增加到16:)
#include <stdio.h>
#include <string.h>
#define MAX 10
#define NAME_LEN 15
// use a typedef to simplify code
typedef struct person {
char name[NAME_LEN];
int age;
} person_t;
// size qualifier on student_list is redundent and person_t* does the same
void input(person_t *student_list, int *total_entries);
int main(void)
{
person_t student_list[MAX];
int total_entries, i;
// pass array and not the non-existent 'student_list[MAX]' element
input(student_list, &total_entries);
for(i=0; i<total_entries; i++)
{
// age is an int, not a string so use %d
printf("Student 1:\tName: %s.\tAge: %d.\n", student_list[i].name, student_list[i].age);
}
return 0;
} //main end
void input(person_t *student_list, int *total_entries)
{
int done = 0, i = 0;
*total_entries = 0;
while (i < MAX) {
printf("Name of student: ");
// use NAME_LEN instead of strlen(list[i].name) because latter is
// probably not initialized at this stage
if (fgets(student_list[i].name, NAME_LEN, stdin) == NULL) {
return;
}
// detect zero-length string
if (student_list[i].name[0] == '\n') {
return;
}
printf("Age of student: ");
scanf("%d", &student_list[i].age);
// read the newline
fgetc(stdin);
*total_entries = ++i;
}
}