º¯Êý·µ»ØֵΪ½á¹¹ÌåÖ¸Õë
#include <stdio.h>#include <stdlib.h>
struct stu {
int num;
int age;
char name;
};
struct stu *get_array_addr(int num) {
return (struct stu *)calloc(num, sizeof(struct stu))
}
void my_setdata(struct stu *p, int num) {
int i = 0;
for (i = 0; i < num; i++) {
printf("ÇëÊäÈëѧÉúÊý¾Ý:");
scanf("%d %d %s", &((p + i) -> num), &((p + i) -> age), (p + i) -> name);
// scanf("%d %d %s", &arr.num, &arr.age, &arr.name);
}
}
void my_printstu(const struct stu *p, int num) {
int i = 0;
for (i = 0; i < num; i++) {
printf("num = %d, age = %d, name = %s\n", p.num, p.age, p.name);
}
}
int main(int argc, char *argv[]) {
int n = 0;
printf("ÇëÊäÈëѧÉú¸öÊý:");
scanf("%d", &n);
struct stu *arr = NULL;
// ¸ù¾ÝѧÉú¸öÊýÉêÇë¶ÑÇø¿Õ¼ä
arr = get_array_addr(n);
if (arr == NULL) {
perror("calloc");
return -1;
}
// »ñÈ¡¼üÅÌÊäÈë
my_setdata(arr, n);
// ±éÀú½á¹¹ÌåÊý×éÄÚÈÝ
my_printstu(arr, n);
// ÊÍ·Å¿Õ¼ä
if (arr != NULL) {
free(arr);
arr = NULL;
}
return 0;
}
±àÒ뱨´í£¬Ìáʾ
struct stu *get_array_addr(int num) {
return (struct stu *)calloc(num, sizeof(struct stu))
}
×îºóµÄ"}"ºóÃæÐèÒª¼Ó";",µ«ÊÇÕâÊÇÒ»¸öº¯Êý°¡£¬ÔõôÈñàÒëÆ÷ÖªµÀËûÊÇÒ»¸ö·µ»Ø½á¹¹ÌåÖ¸ÕëµÄº¯Êý£¬¶ø²»ÊǽṹÌå ÄãµÄ return ºóÃæ©ÁË·ÖºÅ
struct stu *get_array_addr(int num) {
return (struct stu *)calloc(num, sizeof(struct stu));
} zltzlt ·¢±íÓÚ 2020-7-30 09:52
ÄãµÄ return ºóÃæ©ÁË·ÖºÅ
hhhÞÏÞÎÁË
Ò³:
[1]