c - 将结构数组传递给C中的函数
问题描述
通过引用将结构传递给函数时,我是否需要在我的结构数组(在函数参数中)前面有一个 *?我认为我们不这样做的原因是因为数组对于传递第一个对象所在的地址至关重要。
我觉得我很幸运我的代码正在运行:
#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[]){
}
在评论者的帮助下,我对我的代码进行了更多处理,并且我有以下工作正常。我不小心创建了一个指针数组。
#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
解决方案
您拥有的代码是“好的,但是……”。还有一些非常重要的“但是”需要担心。
第一个问题是你写的是否是你打算写的。您已经定义了一个指向结构的指针数组,但根本没有对其进行初始化。您可能打算定义一个结构数组而不是指针数组,这会改变讨论的其余部分。暂时,我把你写的当作“没关系——这就是我打算写的”。
您将数组正确传递给函数。不过,该函数不知道您传递的数组有多大。你应该养成告诉函数数组有多大的习惯。
您不引用函数内部的数组。这并不全是坏事。您还没有定义数组中每个指针指向的内存。您可能会在添加项目时动态分配项目,然后使用箭头->
而不是点正确引用它们.
:
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;
}
如果条目的数量实际上不是固定的,那么readAndUpdate()
函数应该报告有多少被初始化。
我不打算创建一个指针数组。
好的; 那么游戏规则就变了:
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;
}
结构已经分配,并初始化为所有字节为零。函数中的代码使用.
而不是->
引用成员。来自*
变量和参数定义。
推荐阅读
- mysql - 邻接表模型重复父子节点
- apache-flink - Apache Flink 的计划任务
- javascript - 我想知道如何防止 Prettier 以这种奇怪的形式格式化我的 js 代码
- html - 始终定位下一个和上一个顶部
- angular - 使用上下文和drawImage在角度客户端图像压缩上获取黑色图像
- google-apps-script - 可以将循环添加到搜索 Google 日历的 Google 表格脚本吗?
- doxygen - Doxygen:包含带有工作链接的 HTML 页面
- python - ModuleNotFoundError(适用于 bs4),当使用命令行运行在 PyCharm 上创建的 Python 程序时?
- android - 使用 POST 调用 Android 浏览器?
- javascript - 动态添加的下拉菜单不应用 jQuery