[英]passing a struct to a function and loop through it
嘗試將結構傳遞給函數並瀏覽它。 這是將結構傳遞給函數的正確方法嗎? 函數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);
因為您沒有指針數組。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.