c语言如何给指针数组动态赋值,求助,关于动态分配结构体数组后,指针相互赋值的问题,有图有JB...

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

/************************************************************************

2013年6月3日 16:23:38

目的:输入若干个学生信息,按分数的升序进行排序后进行输出

************************************************************************/

#include

#include

struct Student

{

char name[10];

int age;

float score;

};

void inputStudent(struct Student ** point, int nTh); // 输入第 nTh 个学生信息函数

void outputStudent(struct Student ** point, int nTh); // 输出第 nTh 个学生信息函数

void sortStudent(struct Student ** point, int nLen); // 对 nLen 个学生按分数升序排序

int main(void)

{

struct Student * pArr;

int nLen;

int nCnt;

/************************************************************************

输入学生人数并为其分配内存空间

************************************************************************/

printf("请输入学生人数:\n");

scanf("%d", &nLen);

pArr = (struct Student *)malloc(nLen * sizeof(struct Student));

for (nCnt = 0; nCnt < nLen; ++nCnt)

{

printf("请输入第 %d 个学生信息:\n", nCnt + 1);

inputStudent(&pArr, nCnt);

}

sortStudent(&pArr, nLen);

printf(">>>>>>>>>>>输出学生信息>>>>>>>>>>>\n");

for (nCnt = 0; nCnt < nLen; ++nCnt)

{

outputStudent(&pArr, nCnt);

}

return 0;

}

void inputStudent(struct Student ** point, int nTh) // 输入第 nTh 个学生信息函数

{

printf("name: ");

scanf("%s", &(*point + nTh) -> name);

printf("age: ");

scanf("%d", &(*point + nTh) -> age);

printf("score: ");

scanf("%f", &(*point + nTh) -> score);

return ;

}

void outputStudent(struct Student ** point, int nTh) // 输出第 nTh 个学生信息函数

{

printf("第 %d 个学生生信息:\n", nTh + 1);

printf("name: ");

puts((*point + nTh) -> name);

printf("age: %d\n", (*point + nTh) -> age);

printf("score: %.2f\n", (*point + nTh) -> score);

printf("\n");

return ;

}

void sortStudent(struct Student ** point, int nLen) // 对 nLen 个学生按分数升序排序

{

int nCnt1, nCnt2;

// struct Student stTmp;

struct Student * pTmp;

for (nCnt1 = 0; nCnt1 < nLen -1; ++nCnt1)

{

for (nCnt2 = nCnt1 + 1; nCnt2 < nLen; ++nCnt2)

{

if ((*point + nCnt1) -> score > (*point + nCnt2) -> score)

{

/* stTmp = *(*point + nCnt1);

*(*point + nCnt1) = *(*point + nCnt2);

*(*point + nCnt2) = stTmp;

*/

pTmp = (*point + nCnt1);

(*point + nCnt1) = (*point + nCnt2); // error

(*point + nCnt2) = pTmp; // error

}

}

}

return ;

}

代码贴上,请各位领导批示...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值