在C中将结构数组传递给函数
当通过引用将结构传递给函数时,我的结构数组(在函数参数中)前面是否需要*号?我之所以不这么认为,是因为数组必须传递第一个对象所在的地址 我觉得我很幸运我的代码能正常工作:在C中将结构数组传递给函数,c,function,struct,C,Function,Struct,当通过引用将结构传递给函数时,我的结构数组(在函数参数中)前面是否需要*号?我之所以不这么认为,是因为数组必须传递第一个对象所在的地址 我觉得我很幸运我的代码能正常工作: #include <stdio.h> struct member { char lastName[30]; char gender; int age; }; void readAndUpdate(struct member *people[]); // begin main func
#include <stdio.h>
struct member {
char lastName[30];
char gender;
int age;
};
void readAndUpdate(struct member *people[]);
// begin main function
int main(void){
struct member *people[30];
readAndUpdate(people);
} // end main function
// begin function which reads a .dat file and propogates the array with the data in the .dat file
void readAndUpdate(struct member *people[]){
}
#包括
结构成员{
char lastName[30];
性别;
智力年龄;
};
void readAndUpdate(结构成员*people[]);
//开始主要功能
内部主(空){
结构成员*人[30];
阅读和更新(人);
}//结束主函数
//begin函数,用于读取.dat文件并使用.dat文件中的数据传播数组
void readAndUpdate(结构成员*人员[]){
}
在注释者的帮助下,我对我的代码进行了更多的工作,我有以下几点可以正常工作。我意外地创建了一个指针数组
#include <stdio.h>
#define MAXPEOPLE 3
struct member {
char lastName[30];
char gender;
int age;
};
void readAndUpdate(struct member *person, size_t maxpeople);
void populateDatFile();
void displayMembers(struct member *person, size_t maxpeople);
// begin main function
int main(void){
struct member people[2];
populateDatFile(); // program will first populate the .dat file with the given specs
readAndUpdate(people, MAXPEOPLE);
printf("The data was read and input as follows:\n\n");
displayMembers(people, MAXPEOPLE);
} // end main function
// function which displays the entire array of struct members
void displayMembers(struct member *person, size_t maxpeople){
int i=0;
for (i=0;i<3;i++){
printf("%s ", person[i].lastName);
printf("%c ", person[i].gender);
printf("%d ", person[i].age);
printf("\n");
}
} // end displayMembers function
// function which loads the .dat file with hardcoded structs
void populateDatFile(){
struct member person1={"Gates", 'M', 60};
struct member person2={"Jobs", 'M', 55};
struct member person3={"Jane", 'F', 45};
FILE *file;
file = fopen("question3.dat","w");
if(file == NULL)
printf("question3.dat cannot be opened!\n");
else
printf("question3.dat was opened successfully.\n");
fprintf(file, "%s %c %d\n", person1.lastName, person1.gender, person1.age);
fprintf(file, "%s %c %d\n", person2.lastName, person2.gender, person2.age);
fprintf(file, "%s %c %d\n", person3.lastName, person3.gender, person3.age);
fclose(file);
} // end function populateDatFile
// begin function which reads a .dat file and propogates the array with the data in the .dat file
void readAndUpdate(struct member *person, size_t maxpeople){
int i=0;
FILE *file;
file = fopen("question3.dat","r");
if(file == NULL)
printf("question3.dat cannot be opened!\n");
else
printf("question3.dat was opened successfully.\n");
fscanf(file, "%s", &person->lastName);
fscanf(file, " %c", &person->gender);
fscanf(file, "%d", &person->age);
fscanf(file, "%s", &person[1].lastName);
fscanf(file, " %c", &person[1].gender);
fscanf(file, "%d", &person[1].age);
fscanf(file, "%s", &person[2].lastName);
fscanf(file, " %c", &person[2].gender);
fscanf(file, "%d", &person[2].age);
fclose(file);
} // end function readAndUpdate
#包括
#定义MAXPEOPLE 3
结构成员{
char lastName[30];
性别;
智力年龄;
};
无效读取和更新(结构成员*person,大小\u t maxpeople);
void populateDatFile();
void displayembers(结构成员*person,大小\u t maxpeople);
//开始主要功能
内部主(空){
结构成员人[2];
populateDatFile();//程序将首先使用给定的规范填充.dat文件
读取和更新(人,最大人);
printf(“数据的读取和输入如下:\n\n”);
显示成员(人,最大人);
}//结束主函数
//函数,该函数显示结构成员的整个数组
void displayMembers(结构成员*person,大小\u t最大人数){
int i=0;
对于(i=0;ilastName);
fscanf(文件,“%c”,&person->gender);
fscanf(文件,“%d”,&person->age);
fscanf(文件“%s”和人员[1]。姓氏);
fscanf(文件“%c”和人员[1]。性别);
fscanf(文件“%d”,人员[1]。年龄);
fscanf(文件“%s”和人员[2]。姓氏);
fscanf(文件“%c”和人员[2]。性别);
fscanf(文件“%d”和人员[2]。年龄);
fclose(文件);
}//结束函数readAndUpdate
您的代码是“可以,但是…”。还有一些相当重要的“但是”值得担心
第一个问题是你所写的是否是你想要写的。您已经定义了指向结构的指针数组,但根本没有初始化它。您可能打算定义一个结构数组,而不是指针数组,这将改变其余的讨论。目前,我认为你写的是“没关系——这就是我打算写的”
正确地将数组传递给函数。不过,函数不知道传递的数组有多大。你应该养成告诉函数数组有多大的习惯
您不引用函数中的数组。这并不全是坏事;您尚未定义数组中每个指针指向的内存。您可能会在添加项目时动态分配这些项目,然后使用箭头正确地引用它们,而不是点
:
void readAndUpdate(size_t max, struct member *people[max])
{
for (size_t i = 0; i < max; i++)
{
people[i] = malloc(sizeof(*people[i]));
if (people[i] == NULL)
…handle error appropriately…
strcpy(people[i]->lastName, "Unknown");
people[i]->gender = 'N'; // Neuter — unknown
people[i]->age = 0; // Babies only
}
}
int main(void)
{
struct member *people[30] = { NULL };
readAndUpdate(30, people);
return 0;
}
结构已分配,并初始化为所有字节零。函数中的代码使用
而不是->
引用成员。*
来自变量和参数定义。考虑到您的函数没有做任何事情,实际上您是幸运的。您的函数被声明和定义为接受指针数组,而不是指针/数组。类似地,在main
中,您有一个指针数组,而不是结构数组。您有一个指针数组而不是结构数组。我想你想拥有struct number人[30]
然后通过将数组指针与sizereadAndUpdate(struct member*people,size\t maxpeople)
一起传递来填充数组,人员
是指向成员
结构的指针数组,但没有为成员
结构分配实际内存。这可能发生在readAndUpdate()函数中。数组有一个基址和大小,但在函数调用时衰减为指针,在传递给函数时丢失大小信息,如本例所示。搜索StackOverflow以查找指向指针的数组衰减。大量阅读。FWW,C++有着完全相同的行为。在你修改的代码中,你有:<代码> fSCANF(文件,%s),和人>姓氏);fscanf(文件“%s”和人员[1]。姓氏)
为了保持一致性,请在fscanf()
调用的第一个块中使用&person[0].lastName
。然后用循环替换该代码。并测试每个fscanf()
,以确保成功。您还可以将三个连续调用合并为一个:if(fscanf(文件“%s%c%d”),&person[0]。lastName,&person[0]。gender,&person[0]。age)!=3{…oops-错误处理…}
。添加循环时,用循环索引替换0。@Jonathan_Leffler我不想创建指针数组。然而,我已经更新了我的代码,并且它按照预期工作。我会把它编辑到我原来的帖子里
void readAndUpdate(size_t max, struct member people[max])
{
for (size_t i = 0; i < max; i++)
{
strcpy(people[i].lastName, "Unknown");
people[i].gender = 'N'; // Neuter — unknown
people[i].age = 0; // Babies only
}
}
int main(void)
{
struct member people[30] = { { "", 0, 0 } };
readAndUpdate(30, people);
return 0;
}