1.訪問結構體成員,聲明和初始化結構體
一個結構體是一種能同時處理多種數據類型數據的數據類型。 何時為結構體分配內存?
聲明結構體類型時,并不會為結構體分配內存,只有單聲明結構體變量時,才會為結構體聲明類型。
下面通過實例來理解如何創建一個結構體,如何聲明結構體變量,以及如何向結構體變量中讀寫數據。 eg1:編寫一個C程序,創建一個包含多種類型成員的ebill結構體。
/*使用結構體顯示電子賬單*/#include<stdio.h>#include<string.h>void main(){ struct ebill { int mno; char name[20]; int PRevious; int present; int units; float charge; }; struct ebill eb; eb.mno=1005; strcpy(eb.name,"Raj Kumar"); eb.previous=500; eb.present=750; eb.units=eb.present-eb.previous; eb.charge=eb.units*4.50; printf("/nMeter no:%d",eb.mno); printf("/n------name:%s",eb.name); printf("/n-----previous:%d",eb.previous); printf("/n------present:%d",eb.present); printf("/n------units:%d",eb.units); printf("/n------charge:%f",eb.charge);}結果如下:
2.結構體指針
一個結構體指針是指用來引用結構體實例的指針。可以像聲明結構體變量一樣聲明結構體: struct ebill *ptr; 一旦結構體指針被聲明,可以使用結構體指針成員操作符或箭頭操作符來引用結構體實例的數據成員。如: ptr->mno.
3.結構體數組
聲明一個結構體數組:struct ebill eb[50],訪問結構體的成員變量:eb[i].mno;eb[i].name;等。
fprintf()函數: 1.在顯示器上顯示輸出。 fprintf(stdout,”Andhra Pradesh”);stdout代表標準輸出設備,例如顯示器。 顯示一個寬度為20個字符的字符串代碼如下: fprintf(stdout,”%20s”,name); 2.fprintf()函數可以將輸出重定向至打印機。 fprintf(stdprn,”Hyderabad”); stdprn代表連接至計算機的標準打印機,其后緊跟著的字符串將會被發送至打印機,最后打印到紙上。 3.fprintf()也可以將輸出重定向到一個文件,這樣輸出就可以在后續的某個時刻被再次使用。 fprintf(fptr,”%20s”,empname);
4.結構體指針數組
struct ebill *p[50];這里一共50個指針,如果想動態為結構體實例分配內存,可以使用malloc函數,如下: p[i]=(struct ebill *)malloc(sizeof(struct ebill))
5.向函數傳遞結構體參數
可以將整個結構體作為參數傳遞給函數。 eg: struct student { int rno; char name[20]; }
向display( )方法傳遞student結構體,函數聲明如下: void display(struct student st) 這里的st是結構體變量。在函數體內,可以使用st來訪問這個結構體成員,如:st.rno或st.name. 另外一種向函數傳遞結構體類型參數的方法是傳遞結構體指針。eg: void display(struct student *ptr),現在,在函數體內可以這樣引用結構體成員:ptr->rno和ptr->name。 eg:結構體傳參
上面的實例也可以使用結構體指針傳遞結構體實例。 也可以從一個函數返回一個結構體。 為了實現該目的,可以從函數中返回一個結構體指針:struct student * display (struct student *ptr) eg:向函數傳遞結構體指針參數,并從函數返回一個結構體指針。
#include<stdio.h>#include<string.h>struct student{ int rno; char name[20];};struct student s={10,"Priya"};struct student *p1,*p2;struct student *display(struct student *ptr);void main(){ p1=&s; p2=display(p1); printf("/nrno=%d",p2->rno); printf("/nName=%s",p2->name);}struct student *display(struct student *ptr){ ptr->rno++; strcpy(ptr->name,"Lakshmi"); return ptr;}6.拷貝結構體變量 可以將一個結構體實例中的數據拷貝到另外一個結構體實例中,只要兩者類型相同。
struct employee{ int id; char name;}struct employee e1,e2;這里e1,e2是結構體變量。我們想將e1的內容拷貝到e2中。有兩種方式可以實現該目的: 1.將e1的數據成員逐個拷貝到e2中: e2.id=e1.id; strcpy(e2.name,e1.name); 2.將一個結構體變量賦值給另一個結構體變量。使用方式如下: e2=e1. 將整個結構體的實例賦值給另一個結構體的實例是可行的,但是對于數組不可以這樣操作。如果想要將一個數組中的元素拷貝到另外一個數組中,需要逐個處理數組中的元素。
#include<stdio.h>#include<string.h>struct student{ int rno; char name[20];};struct student s={10,"Priya"};void main(){struct student s={10,"Priya"};struct student y; y=s; printf("/nId=%d",y.rno); printf("/nName=%s",y.name);}結構體和數組的區別: 1)數組只能存儲同一類型的數據。結構體可以存儲不同類型的數據。 2)如果想將一個數組的元素拷貝至另外一個數組,則需要逐個元素拷貝。而將一個結構體實例的數據拷貝至另外一個結構體實例,只需要逐個拷貝或者直接將結構體變量賦值給另外一個結構體變量。
7.嵌套結構體 可以在一個結構體里面包含另外一個結構體。
1)雇員的id和名字。struct employee {int id;char name[20];}2)雇員生日的年月日。struct dob{ int dd; int mm; int yy; }code如下:
struct employee{ int id; char name[20]; struct dob d; }; struct employee e;結構體變量既可以當作左值也可以當作右值來處理。
編寫一個結構體函數用來查找系統日期和時間。
#include<stdio.h>#include<dos.h>void main(){ struct data d; struct time t; getdata(&d); gettime(&t); printf("/n%2d/%2d/%2d",d.da_day,d.da_mon,d.da_year); printf("/n%2d:%2d:%2d",t.ti_hour,t.ti_min,t.ti_sec);}10.聯合體
聯合體與結構體比較類似,用關鍵字union來聲明。 一個聯合體可以有任意多個數據成員,但是只為一個聯合體實例分配與最大數據成員所占內存大小相等的內存塊,其他較小的數據成員也存放在該內存塊中。從聯合體的內存布局來看,它比結構體更節省內存,因為所有的數據成員都存放在同一塊內存中。這樣做的后果是,每次只能向聯合體實例中存儲一個數據成員,并且會覆蓋之前放在該內存塊的其他數據成員。
新聞熱點
疑難解答