c语言指向结构体的指针
A. c语言中指向结构体变量的指针,这样写对吗
pT_t
确实是指向
结构体
变量的指针,但其实它是一个类型,就跟
int,int*这些类似
pT_t
ptest
=
NULL;
的效果,其实等同于
T_t
*ptest
=
NULL;
B. 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;
}
C. c语言结构体指针
既然已经声明了结构体指针,那么编译器就会认为这个指针指向的对象符合你所给出的结构体的构成,因此顺着地址摸过去再按照结构体解析。
比方说,给你个203房间的钥匙,虽然你并不清楚203房间具体的位置,但是你也会知道房间里至少有床可供休息、有厕所可供便溺,当你恰好有便意的时候就会找到203房间去上厕所……
D. 急求急急急C语言结构体的结构指针!
简单讲两个概念:
1、数组名表示数组首地址。
2、运算符->优先级高于*。
所以*pt->y就相当于*(pt->y)。
由于pt==c==&c[0];
所以pt->y就是结构体变量{10,a}->y就是a。
a也是数组名,所以就是&a[0];
所以*pt->y就是*(&a[0])就是a[0]就是1。
另外补充,你结构数组初始化这样写不太标准,建议把数组元素也用大括号括起来,像这样struct st c[2]={{10,a},{20,b}};
E. 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));
}
}
}
}
F. 在C语言中关于指向结构体指针的问题
第一句只是得到了一个指针 没做任何操作
第二句是把P1这个指针指向后一个节点,是对P1赋了新值
G. 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;
H. C语言结构体指针
->的优先级高于++,
所以++pt–>x,是把x的值加1
I. 文件指针 跟 指向结构体的指针 有什么区别(C语言)
文件指针是指向struct
FILE的指针(类型为struct
FILE*),是指向结构体的指针的其中一种。
FILE是C语言中默认用来表示文件信息的结构体,在stdio.h中有定义,具体内容和运行环境(平台)相关。当打开一个文件时,文件内容由库函数记录到一个FILE结构体中,而用户可以通过库函数返回的struct
FILE*类型的指针对文件进行间接操作。
此外FILE结构体中含有文件的读写指针(LS误把这个当作文件指针了),用于指示文件的读写位置,一般是不直接访问的(通过fseek、ftell、rewind等读取或写入)。
J. c语言指向结构体的指针
首先你的linkman是struct person的数组
linkman本身就是个地址
可以直接传递到函数里的
如果你要用指针的话,就需要一个struct的二维指针,或者指针数组,如struct person **p;或者struct person (*p)[200];
还是建议你直接使用linkman传递地址。
另外,你要用linkman里面的name的话,如果是当字符串来使用的话,直接linkman[1]->name就可以了