尝试将结构传递给函数并浏览它。这是将结构传递给函数的正确方法吗?函数 view() 中的 for 循环似乎不起作用。任何有关这方面的帮助也将不胜感激。
我的结构:
typedef struct {
char name[20];
employee *list_employees;
int empl_count;
} agency;
typedef struct {
char name[30];
int age;
} employee;
代码的重要部分:
int main()
{
//...
int nmbr_agencies;
agency *list_agencies = malloc(sizeof(agency) * nmbr_agencies);
view(&list_agencies, &nmbr_agencies);
}
void view(agence *ListA[], int *nmbr)
{
int i=0;
for (i = 0; i < *nmbr; i++){
printf("name of agency: %s\n", ListeA[i]->name);
printf("number of employees\n, ListeA[i]->empl_count);
}
}
最佳答案
没有。
如果您有的话,您应该只传递一个数组,而不是假装(在调用中)您有一个您没有的指针数组。
创建函数:
void view(const agency *list, size_t number);
并这样调用它:
view(list_agencies, nmbr_agencies);
然后在函数内部,进行直接访问:
printf("name of agency: %s\n", list[i].name);
因为您没有指针数组。
关于c - 将结构体传递给函数并循环遍历它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27744842/