要賦值C語言結構體數組,可以通過以下幾種方式:
struct student {
char name[20];
int age;
};
int main() {
struct student arr[3];
strcpy(arr[0].name, "Tom");
arr[0].age = 20;
strcpy(arr[1].name, "Jerry");
arr[1].age = 22;
strcpy(arr[2].name, "Alice");
arr[2].age = 21;
return 0;
}
struct student {
char name[20];
int age;
};
int main() {
struct student arr[3] = {
{"Tom", 20},
{"Jerry", 22},
{"Alice", 21}
};
return 0;
}
struct student {
char name[20];
int age;
};
int main() {
struct student arr[3];
int i;
for (i = 0; i < 3; i++) {
printf("請輸入第%d個學生的姓名:", i + 1);
scanf("%s", arr[i].name);
printf("請輸入第%d個學生的年齡:", i + 1);
scanf("%d", &arr[i].age);
}
return 0;
}