c语言指针指向结构体
① c语言如何用指针指向结构体数组
#include<stdio.h>
int main()
{ struct st
{ int id;
char name[20];
}*p,stu[10];
int i;
for(p=stu; p<stu+3; p++)
scanf("%d%s",&p->id,p->name);
for(p=stu; p<stu+3; p++)
printf("%d %s ",p->id,p->name);
return 0;
}
② 指向结构体的指针应该怎么理解
当一个指针变量指向结构体时,我们就称它为结构体指针。注意,结构体变量名和数组名不同,数组名在表达式中会被转换为数组指针,而结构体变量名不会,无论在任何表达式中它表示的都是整个集合本身,要想取得结构体变量的地址,必须在前面加&,所以给 pstu 赋值只能写作:
struct stu *pstu = &stu1;
而不能写作:
struct stu *pstu = stu1;
还应该注意,结构体和结构体变量是两个不同的概念:结构体是一种数据类型,是一种创建变量的模板,编译器不会为它分配内存空间,就像 int、float、char 这些关键字本身不占用内存一样;结构体变量才包含实实在在的数据,才需要内存来存储。
下面的写法是错误的,不可能去取一个结构体名的地址,也不能将它赋值给其他变量:
struct stu *pstu = &stu;
struct stu *pstu = stu;
(2)c语言指针指向结构体扩展阅读
结构体指针作为函数参数
结构体变量名代表的是整个集合本身,作为函数参数时传递的整个集合,也就是所有成员,而不是像数组一样被编译器转换成一个指针。
如果结构体成员较多,尤其是成员为数组时,传送的时间和空间开销会很大,影响程序的运行效率。所以最好的办法就是使用结构体指针,这时由实参传向形参的只是一个地址,非常快速。
调用中采用的结构体变量。在传入函数时通过指针void *para指针传递过去。需要注意的是不能直接使用para->a来访问结构体的成员。
这是因为para只是接收过来的地址。para虽然指向的结构体的首地址。但是这个指针并不知道自己指向的是什么内容和有多少成员。需要(date *)para强制转化一下。这样para就可以知道自己是什么类型的指针。有多少成员。
③ C语言,利用指向结构体的指针编程
#include<stdio.h>
#include<stdlib.h>
#include<string>
#defineSTUDENT_NUM3//需要输入的学生总数
typedefstruct
{
intnNumber;//学号
charchName[32];//姓名
floatMidScore;//期中成绩
floatEndScore;//期末成绩
floatAverScore;//平均成绩
}STUDENT_UNIT;
/*
brief:成绩排序
para:pStudent指向结构体的指针
nNum: 需要排序的个数
return:无
*/
voidSortByScore(STUDENT_UNIT*pStudent,intnNum);
intmain()
{
STUDENT_UNITStudent[STUDENT_NUM]={0,0,0};
intnSort[STUDENT_NUM]={0};
for(inti=0;i<STUDENT_NUM;i++)
{
printf("请输入学号:");
scanf("%d",&Student[i].nNumber);
//printf(" ");
printf("请输入姓名:");
scanf("%s",&Student[i].chName);
//printf(" ");
printf("请输入期中成绩:");
scanf("%f",&Student[i].MidScore);
//printf(" ");
printf("请输入期末成绩:");
scanf("%f",&Student[i].EndScore);
printf(" ");
Student[i].AverScore=(Student[i].MidScore+Student[i].EndScore)/2.0;
}
SortByScore(&Student[0],STUDENT_NUM);//排序
//输出
for(inti=0;i<STUDENT_NUM;i++)
{
printf("学号%d 姓名%s 期中成绩%.1f期末成绩%.1f 平均成绩%.1f ",
Student[i].nNumber,Student[i].chName,Student[i].MidScore,Student[i].EndScore,Student[i].AverScore);
}
return0;
}
voidSortByScore(STUDENT_UNIT*pStudent,intnNum)
{
STUDENT_UNITTempStudent={0};
for(inti=0;i<nNum-1;i++)
{
for(intj=i+1;j<nNum;j++)
{
if((pStudent+i)->AverScore<(pStudent+j)->AverScore)
{
memset((char*)&TempStudent,0,sizeof(STUDENT_UNIT));
memcpy((char*)&TempStudent,(char*)(pStudent+i),sizeof(STUDENT_UNIT));
memset((char*)(pStudent+i),0,sizeof(STUDENT_UNIT));
memcpy((char*)(pStudent+i),(char*)(pStudent+j),sizeof(STUDENT_UNIT));
memset((char*)(pStudent+j),0,sizeof(STUDENT_UNIT));
memcpy((char*)(pStudent+j),(char*)&TempStudent,sizeof(STUDENT_UNIT));
}
}
}
}
④ C语言指向结构体的指针
C语言中的结构体
在C语言中,结构体(struct)指的是一种数据结构,是C语言中聚合数据类型(aggregate data type)的一类。结构体可以被声明为变量、指针或数组等,用以实现较复杂的数据结构。结构体同时也是一些元素的集合,这些元素称为结构体的成员(member),且这些成员可以为不同的类型,成员一般用名字访问。定义与声明
结构体的定义如下所示,struct为结构体关键字,tag为结构体的标志,member-list为结构体成员列表,其必须列出其所有成员;variable-list为此结构体声明的变量。结构体成员的引用
有两种方式,英文的句点 . ,一个减号加一个大于号 ->。
当结构体是一个指针时要引用结构体的成员就用-> 而如果不是指针就用.。
如:
struct msg_st {
int a;
};
struct msg_st msg;
struct msg_st *ms;
msg.a = 10;
ms->a = 20;
⑤ c语言关于指向结构体的指针的理解
Polynomial *p
那p就是一个指向“PolyNode结构体指针”的指针。
typedef struct {
...
}*Polynomial;
这说明Polynomial是定义指针类型的,指向对象是这个结构体。
Polynomial p;就是定义了一个指向这种结构体的指针。
⑥ c语言指向结构体的指针
首先你的linkman是struct person的数组
linkman本身就是个地址
可以直接传递到函数里的
如果你要用指针的话,就需要一个struct的二维指针,或者指针数组,如struct person **p;或者struct person (*p)[200];
还是建议你直接使用linkman传递地址。
另外,你要用linkman里面的name的话,如果是当字符串来使用的话,直接linkman[1]->name就可以了
⑦ C语言指向结构体变量的指针
#include<stdio.h>
structfa
{
intnumber;
charname[20];
charsex;
intage;
floatscore;
};
intmain()
{
structfaa[1]={1001,"lee",'m',25,90.3};
structfa*q;
q=&a[0];
printf("Number=%d ",(*q).number);
printf("Name=%s ",(*q).name);
printf("Sex=%c ",(*q).sex);
printf("Age=%d ",(*q).age);
printf("Score=%f ",(*q).score);
}
简单的就是这样。先定义结构体,然后给机构体进行赋值,把结构体的地址存储到指针。最后通过指针输出结构题的数据
⑧ C语言编程指向结构体数组的指针
下面的程序是我以前写的,你稍微改改就能符合你的要求了
#include<stdio.h>
#include<malloc.h>
typedefstructst
{
charname[20];
intchinese;
intmath;
intenglish;
floataverage;
}student;
voidswap(student*a,student*b)
{
studenttemp=*a;
*a=*b;
*b=temp;
}
voidsort(student*array,intn)
{
inti,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;
}
}
voidprint(student*array,intn)
{
inti;
printf("姓名 语文 数学 英语 平均成绩 ");
for(i=0;i<n;i++)
{
printf("%s %d %d %d %f ",array[i].name,array[i].chinese,
array[i].math,array[i].english,array[i].average);
}
}
intmain()
{
intnumber=9;
inti;
student*stu=(student*)malloc(sizeof(student)*9);
for(i=0;i<number;i++)
{
printf("请输入第%d个学生的姓名及成绩(姓名语文数学英语成绩以空格隔开): ",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);
return0;
}