生活随笔
收集整理的這篇文章主要介紹了
Linux C 深入分析结构体指针的定义与引用
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
關(guān)于結(jié)構(gòu)體的基礎(chǔ)知識(shí),網(wǎng)上書(shū)上都一大堆,這里就不贅述了,下面我們要學(xué)習(xí)的是結(jié)構(gòu)體指針。
介紹結(jié)構(gòu)體指針之前,先給大家看一個(gè)小程序:
[cpp]?view plaincopy
#include?<stdio.h>?? #include?<string.h>?? #include?<malloc.h>?? ?? struct?Man?? {?? ????char?name[10];?? };?? ?? int?main()?? {?? ????struct?Man?N;?? ????N.name?=?"qiang";?? ????printf("%s\n",N.name);?? }??
這段程序很簡(jiǎn)單,就是給結(jié)構(gòu)體成員賦值,這里結(jié)構(gòu)體成員是個(gè)數(shù)組,大家看看這種賦值方式有沒(méi)有錯(cuò),我們編譯一下:
[cpp]?view plaincopy
fs@ubuntu:~/qiang/struct$?gcc?-o?struct4?struct4.c?? struct4.c:?In?function?‘main’:?? struct4.c:13:9:?error:?incompatible?types?when?assigning?to?type?‘char[10]’?from?type?‘char?*’?? fs@ubuntu:~/qiang/struct$???
13行報(bào)錯(cuò),就是賦值那行,報(bào)錯(cuò)原因是“字符分配的類(lèi)型是不兼容的類(lèi)型”
我們看看這句N(xiāo).name = "qiang",右邊是字符串常量,這里其實(shí)是字符串的首地址,就是一個(gè)地址,我們以前 char a[] = "qiang"沒(méi)錯(cuò)啊,為什么這里報(bào)錯(cuò)了,我們看看左值,N.name, name 是數(shù)組名,是代表數(shù)組的首地址啊,但是我們要記住,這里name是個(gè)地址常量,是不能給常量賦值的,所以會(huì)報(bào)錯(cuò),那我們?nèi)绾谓o一個(gè)結(jié)構(gòu)體中的字符數(shù)組賦值呢?我們這里用strcpy(N.name,"qiang") ! 當(dāng)然我們N.name[1] = 'q',這樣是可以的。
下面開(kāi)始講結(jié)構(gòu)體指針:
一、指向結(jié)構(gòu)體類(lèi)型變量的使用
首先讓我們定義結(jié)構(gòu)體:
[cpp]?view plaincopy
<span?style="color:#000000;">struct?stu?? {?? ????char?name[20];?? ????long?number;?? ????float?score[4];?? };?? </span>??
再定義指向結(jié)構(gòu)體類(lèi)型變量的指針變量:
struct stu *p1, *p2 ;
定義指針變量p1、p2,分別指向結(jié)構(gòu)體類(lèi)型變量。引用形式為:指針變量→成員;這里我們要注意,非結(jié)構(gòu)體指針引用類(lèi)型是? 結(jié)構(gòu)體類(lèi)型變量 . 成員;
下面我們看一個(gè)例子:
對(duì)指向結(jié)構(gòu)體類(lèi)型變量的正確使用。
?輸入一個(gè)結(jié)構(gòu)體類(lèi)型變量的成員,并輸出:
[cpp]?view plaincopy
#include?<stdlib.h>??? #include?<stdio.h>?? ?? struct?data??? {?? ????int?day,month,year;?? };?? ?? struct?stu??? {?? ????char?name[20];?? ????long?num;?? ????struct?data?birthday;??? };?? ?? int?main()??? {?? ????struct?stu?*student;??? ????student?=?malloc(sizeof(struct?stu));??? ????printf("Input?name,number,year,month,day:\n");?? ????scanf("%s",student->name);??? ????scanf("%ld",&student->num);?? ????scanf("%d%d%d",&student->birthday.year,&student->birthday.month,?? ????????????&student->birthday.day);?? ?????? ????printf("\nOutputname,number,year,month,day\n");?? ?? ????printf("%8s????%5ld??%d//%d//%d\n",student->name,student->num,?? ????????student->birthday.year,student->birthday.month,?? ????????student->birthday.day);?? }??
執(zhí)行結(jié)果如下:
[cpp]?view plaincopy
fs@ubuntu:~/qiang/struct/tmp$?./struct1?? Input?name,number,year,month,day:?? xiao?? 10086?? 2012?? 12?? 22?? ?? Outputname,number,year,month,day?? ????xiao????10086??2012?? fs@ubuntu:~/qiang/struct/tmp$???
程序中使用結(jié)構(gòu)體類(lèi)型指針引用結(jié)構(gòu)體變量的成員,需要通過(guò)C提供的函數(shù)malloc()來(lái)為指針?lè)峙浒踩牡刂贰:瘮?shù)sizeof()返回值是計(jì)算給定數(shù)據(jù)類(lèi)型所占內(nèi)存的字節(jié)數(shù)。指針?biāo)父鞒蓡T形式為:
[cpp]?view plaincopy
student->name?? student->num?? student->birthday.year?? student->birthday.month?? student->birthday.day??
?
二、指向結(jié)構(gòu)體類(lèi)型數(shù)組的指針的使用
??????? 定義一個(gè)結(jié)構(gòu)體類(lèi)型數(shù)組,其數(shù)組名是數(shù)組的首地址,這一點(diǎn)前面的課程介紹得很清楚。
??????? 定義結(jié)構(gòu)體類(lèi)型的指針,既可以指向數(shù)組的元素,也可以指向數(shù)組,在使用時(shí)要加以區(qū)分。
上個(gè)例子中定義了結(jié)構(gòu)體類(lèi)型,根據(jù)此類(lèi)型再定義結(jié)構(gòu)體數(shù)組及指向結(jié)構(gòu)體類(lèi)型的指針
[cpp]?view plaincopy
struct?data?? {?? intday,month,year;?? };?? struct?stu?? {?? char?name[20];?? long?num;?? struct?data?birthday;?? };??
[cpp]?view plaincopy
struct?stustudent[4],*p;?????
使p=student,此時(shí)指針p就指向了結(jié)構(gòu)體數(shù)組student。
p是指向一維結(jié)構(gòu)體數(shù)組的指針,對(duì)數(shù)組元素的引用可采用三種方法。
1)地址法
? student+i和p+i均表示數(shù)組第i個(gè)元素的地址,數(shù)組元素各成員的引用形式為:
(student+i)->name、(student+i)->num和(p+i)->name、(p+i)->num等。student+i和p+i與&student[i]意義相同。
2)指針?lè)?br /> 若p指向數(shù)組的某一個(gè)元素,則p++就指向其后續(xù)元素。
3)指針的數(shù)組表示法
若p=student,我們說(shuō)指針p指向數(shù)組student,p[i]表示數(shù)組的第i個(gè)元素,其效果與student[i]等同。對(duì)數(shù)組成員的引用描述為:p[i].name、p[i].num等
指向結(jié)構(gòu)體數(shù)組的指針變量的使用:
[cpp]?view plaincopy
#include?<stdio.h>?? #include?<malloc.h>?? ?? struct?data?? {?? ????int?year,month,day;?? };?? ?? struct?stu?? {?? ????char?name[20];?? ????long?num;?? ????struct?data?birthday;?? };?? ?? int?main()?? {?? ????int?i;?? ????struct?stu?*p,student[4]={{"liying",1,1978,5,23},{"wangping",2,1979,3,14},?? ????????{"libo",3,1980,5,6},{"xuyan",4,1980,4,21}};?? ?????? ????p?=?student;?? ????printf("Outputname,number,year,month,day\n");?? ????for(i?=?0;i?<?4;i++)?? ????????printf("%8s?%6ld???%d//%d//%d\n",(p+i)->name,(p+i)->num,?? ????????????????(p+i)->birthday.year,(p+i)->birthday.month,?? ????????????????(p+i)->birthday.day);?? ?? ????return?0;?? }??
執(zhí)行結(jié)果如下:
[cpp]?view plaincopy
fs@ubuntu:~/qiang/struct/tmp$?./struct2?? Outputname,number,year,month,day?? ??liying??????1???1978?? wangping??????2???1979?? ????libo??????3???1980?? ???xuyan??????4???1980?? fs@ubuntu:~/qiang/struct/tmp$???
?
附:給大家看一個(gè)有意思的程序:
寫(xiě)出一個(gè)模擬時(shí)鐘程序
分析:我們知道時(shí)間有時(shí) 分 秒 組成,這里用結(jié)構(gòu)體表示
代碼如下:
[cpp]?view plaincopy
#include?<stdio.h>?? #include?<unistd.h>?? #include?<malloc.h>?? #include?<string.h>?? ?? typedef?struct?Clock?? {?? ????int?hour;?? ????int?minute;?? ????int?second;?? }Clock;?? ?? update(Clock?*p)?? {?? ????p->second++;?? ?? ????if(p->second?==?60)?? ????{?? ????????p->second?=?0;?? ????????p->minute++;?? ????}?? ?? ????if(p->minute?==?60)?? ????{?? ????????p->minute?=?0;?? ????????p->hour++;?? ????}?? ????if(p->hour?==?24)?? ????????p->hour?=?0;?? }?? ?? Display(Clock?*p)?? {?? ????printf("\r%02d:%02d:%02d",p->hour,p->minute,p->second);?? ????fflush(stdout);?? }?? ?? int?main()?? {?? ????Clock?*clock;?? ????clock?=?(Clock?*)malloc(sizeof(Clock));?? ????memset(clock,'\0',sizeof(Clock));?? ?? ????while(1)?? ????{?? ????????sleep(1);?? ????????update(clock);?? ????????Display(clock);?? ????}?? ????free(clock);?? ????return?0;?? }??
執(zhí)行結(jié)果如下:
[cpp]?view plaincopy
fs@ubuntu:~/qiang/struct$?./clock?? 00:00:01??
[cpp]?view plaincopy
fs@ubuntu:~/qiang/struct$?./clock?? 00:00:55??
這里是個(gè)動(dòng)態(tài)效果,大家可以打印出來(lái)看一下
總結(jié)
以上是生活随笔為你收集整理的Linux C 深入分析结构体指针的定义与引用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。