在此程序中,我想定義一個名爲person的結構,並在元素的未使用空間中插入一個元素的插入函數作爲類型的人。最後,我想將結果打印爲標準輸出。任何人都可以給我一個正確的錯誤提示嗎?乾杯聲明結構類型,將值插入到該類型的數組中,並打印輸出數組
錯誤:
arrays.c:16:22: error: expected ')' before '[' token
arrays.c: In function 'main':
arrays.c:34:5: warning: implicit declaration of function 'insert'
arrays.c:41:5: warning: format '%s' expects type 'char *', but argument 2 has type 'char **'
代碼
#include <stdio.h>
/* these arrays are just used to give the parameters to 'insert',
to create the 'people' array */
char *names[7]= {"Simon", "Suzie", "Alfred", "Chip", "John", "Tim",
"Harriet"};
int ages[7]= {22, 24, 106, 6, 18, 32, 24};
/* declare your struct for a person here */
typedef struct{
char name;
int ages;
} person;
static void insert (p[], char *name, int ages) {
static int nextfreeplace = 0;
/* put name and age into the next free place in the array parameter here */
person p[0] = {&name, age};
/* modify nextfreeplace here */
nextfreeplace++;
}
int main(int argc, char **argv) {
/* declare the people array here */
person p[7];
//insert the members and age into the unusage array.
for (int i=0; i < 7; i++) {
insert (p[i], &names[i], ages[i]);
p[i]= p[i+1];
}
/* print the people array here*/
for (int i=0; i < 7; i++) {
printf("%s is %d years old\n", &names[i], ages[i]);
}
return 0;
}
'nextfreeplace'變量的位置是可疑的。您需要知道數組中有多少行(這是變量記錄的內容),用於循環數據等,並刪除它們。除了當前的問題,您可能需要使其更加明顯。此外,你將無法清空數組;你也不能有兩個單獨的由該函數管理的人員陣列。 –