动态结构体和函数的问题
搞了好几天也没搞明白函数的参数要怎么设定。程序代码:
#include <stdio.h> #include <stdlib.h> void k((*pArr[0]).score, int len); struct Student { int age; float score; char name[100]; }; int main (void) { int len; struct Student * pArr; int i; int j; k(&pArr[0].score, len); printf("请输入学生个数:\n"); printf("amount="); scanf_s("%d", &len); pArr = (struct Student *)malloc(len * sizeof(struct Student)); //输入 for (i = 0; i < len; i++) { printf("请输入第%d个学生的信息:\n", i+1); printf("age = "); scanf_s("%d", &pArr[i].age); printf("name = "); scanf_s("%s", pArr[i].name, 10); /* 1.name是数组名,本身就已经是数组首元素的地址,所以pArri[i].name不能写成&pArr[i].name 2.由于scanf_s的防溢出机制,所以这个地方要加上字符位数,但此特性只有微软实行,所以可移植性会降低。 */ printf("score ="); scanf_s("%f", &pArr[i].score); } // 输出 printf("你输入的信息是:\n\n\n"); for (i = 0; i < len; ++i) { printf("第%d个学生的信息是:\n", i + 1); printf("age = %d\n", pArr[i].age); printf("name = %s\n", pArr[i].name); printf("score =%f\n", pArr[i].score); } return 0; } void k((*pArr[0]).score, int len;) { struct Student t; int i; for (i = 0; i < len - 1; i++) { if (pArr[i].score > pArr[i - 1].score) { t = pArr[i]; pArr[i] = pArr[i - 1]; pArr[i - 1] = t; } } }