c语言结构体指针数组,c语言指针与数组详解

http://www.itjxue.com  2023-01-20 19:44  来源:未知  点击次数: 

C语言结构体指针数组怎么声明

我说明写在案例的备注里,你参考吧。

#includestdio.h

typedef?struct?st

{

????????int?id;

}ST,*STP;?//先定义类型???ST是结构类型???STP是结构指针类型

int?main()

{

????STP?st[2];//这里st就是你要的结构指针数组

????ST?st1,st2;//这里我定义了2个结构变量,并赋值,让指针数组的元素分别指向这两个变量地址

????st1.id=1;st2.id=2;

????st[0]=st1;

????st[1]=st2;

????printf("%d,%d\n",st[0]-id,st[1]-id);//打印指针数组指向地址内容

????return?0;

}

C语言编程指向结构体数组的指针

下面的程序是我以前写的,你稍微改改就能符合你的要求了

#include?stdio.h

#include?malloc.h

typedef?struct?st

{

????char?name[20];

????int?chinese;

????int?math;

????int?english;

????float?average;

}student;

void?swap(student?*a,?student?*b)

{

????student?temp?=?*a;

????*a?=?*b;

????*b?=?temp;

}

void?sort(student?*array,?int?n)

{

????int?i,?j,?flag;

????for?(i?=?0;?i??n?-?1;?i++)

????{

????????flag?=?1;

????????for?(j?=?0;?j??n?-?i?-?1;?j++)

????????{

????????????if?(array[j].average??array[j?+?1].average)

????????????{

????????????????swap(array?+?j,?array?+?j?+?1);

????????????????flag?=?0;

????????????}

????????}

????????if?(flag)

????????????break;

????}

}

void?print(student?*array,?int?n)

{

????int?i;

????printf("姓名\t语文\t数学\t英语\t平均成绩\n");

????for?(i?=?0;?i??n;?i++)

????{

????????printf("%s\t%d\t%d\t%d\t%f\n",?array[i].name,?array[i].chinese,

????????????array[i].math,?array[i].english,?array[i].average);

????}

}

int?main()

{

????int?number?=?9;

????int?i;

????student?*stu?=?(student?*)malloc(sizeof(student)?*?9);

????for?(i?=?0;?i??number;?i++)

????{

????????printf("请输入第%d个学生的姓名及成绩(姓名?语文?数学?英语成绩以空格隔开):\n",?i?+?1);

????????scanf("%s?%d?%d?%d",?(*(stu?+?i)).name,?(*(stu?+?i)).chinese,?

????????????(*(stu?+?i)).math,?(*(stu?+?i)).english);

????????(*(stu?+?i)).average?=?((*(stu?+?i)).chinese?+?

????????????(*(stu?+?i)).math?+?(*(stu?+?i)).english)?/?(float)3.0;

????}

????print(stu,?number);

????sort(stu,?number);

????print(stu,?number);

????free(stu);

????return?0;

}

C语言编程问题:结构体数组及指针

struct student *list;

int count = ReadStudentInfo("假设这是文件名", list);

这个函数是这样被调用。

里面应该这样写

int ReadStudentInfo(const char* filename, struct student** pStudents)

{

*pStudents = (struct student*)malloc(sizeof(** pStudents) * n);//假设有n个学生

}

C语言的指针结构体数组问题

#include?stdio.h

#include?string.h

struct?stu{

int?num;

char?name[20];

char?sex;

float?score;

}boy[5]={{101,"li?ping",'m',45},

{102,"zhang?ping",'m',62.5},

{103,"he?fang",'m',92.5},

{104,"cheng?ling",'f',87},

{106,"wang?ming",'m',58}};

struct?node

{

struct?stu?num;

struct?node?*next;

};

main()?

{?

int?i=0,bjg=0;

struct?node?s[5]={0};

for?(i=0;i5;i++)

{

s[i].num.num?=?boy[i].num;

strcpy(s[i].num.name,boy[i].name);

s[i].num.sex?=?boy[i].sex;

s[i].num.score?=boy[i].score;

s[i].next=s[i+1];

}

s[4].next=NULL;

printf("不及格:\n");

for?(i=0;i5;i++)

{

if?(s[i].num.score60)

{

bjg++;

printf("[%d]?[%s]?[%c]?[%.2f]\n",s[i].num.num,s[i].num.name,s[i].num.sex,s[i].num.score);

}

}

printf("不及格人数【%d】\n",bjg);

}

不及格:

[101]?[li?ping]?[m]?[45.00]

[106]?[wang?ming]?[m]?[58.00]

不及格人数【2】

Press?any?key?to?continue

不贴代码我就不调了?自己写了个

不说别的?光是malloc也不对啊??你不malloc?struct?node吗?

不malloc?struct?node?你的next指针存哪里?

至于链表赋值我就不看了

(责任编辑:IT教学网)

更多

推荐PHP+MySQL视频文章