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;
}