日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

Linux C 深入分析结构体指针的定义与引用

發布時間:2023/12/9 linux 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Linux C 深入分析结构体指针的定义与引用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

關于結構體的基礎知識,網上書上都一大堆,這里就不贅述了,下面我們要學習的是結構體指針。

介紹結構體指針之前,先給大家看一個小程序:

[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);??
  • }??
  • 這段程序很簡單,就是給結構體成員賦值,這里結構體成員是個數組,大家看看這種賦值方式有沒有錯,我們編譯一下:

    [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行報錯,就是賦值那行,報錯原因是“字符分配的類型是不兼容的類型”
    我們看看這句N.name = "qiang",右邊是字符串常量,這里其實是字符串的首地址,就是一個地址,我們以前 char a[] = "qiang"沒錯啊,為什么這里報錯了,我們看看左值,N.name, name 是數組名,是代表數組的首地址啊,但是我們要記住,這里name是個地址常量,是不能給常量賦值的,所以會報錯,那我們如何給一個結構體中的字符數組賦值呢?我們這里用strcpy(N.name,"qiang") ! 當然我們N.name[1] = 'q',這樣是可以的。

    下面開始講結構體指針:

    一、指向結構體類型變量的使用

    首先讓我們定義結構體:

    [cpp]?view plaincopy
  • <span?style="color:#000000;">struct?stu??
  • {??
  • ????char?name[20];??
  • ????long?number;??
  • ????float?score[4];??
  • };??
  • </span>??

  • 再定義指向結構體類型變量的指針變量:
    struct stu *p1, *p2 ;
    定義指針變量p1、p2,分別指向結構體類型變量。引用形式為:指針變量→成員;這里我們要注意,非結構體指針引用類型是? 結構體類型變量 . 成員;

    下面我們看一個例子:

    對指向結構體類型變量的正確使用。

    ?輸入一個結構體類型變量的成員,并輸出:

    [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);??
  • }??
  • 執行結果如下:

    [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//12//22??
  • fs@ubuntu:~/qiang/struct/tmp$???
  • 程序中使用結構體類型指針引用結構體變量的成員,需要通過C提供的函數malloc()來為指針分配安全的地址。函數sizeof()返回值是計算給定數據類型所占內存的字節數。指針所指各成員形式為:

    [cpp]?view plaincopy
  • student->name??
  • student->num??
  • student->birthday.year??
  • student->birthday.month??
  • student->birthday.day??
  • ?

    二、指向結構體類型數組的指針的使用
    ??????? 定義一個結構體類型數組,其數組名是數組的首地址,這一點前面的課程介紹得很清楚。
    ??????? 定義結構體類型的指針,既可以指向數組的元素,也可以指向數組,在使用時要加以區分。

    上個例子中定義了結構體類型,根據此類型再定義結構體數組及指向結構體類型的指針

    [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,此時指針p就指向了結構體數組student。
    p是指向一維結構體數組的指針,對數組元素的引用可采用三種方法。
    1)地址法
    ? student+i和p+i均表示數組第i個元素的地址,數組元素各成員的引用形式為:
    (student+i)->name、(student+i)->num和(p+i)->name、(p+i)->num等。student+i和p+i與&student[i]意義相同。
    2)指針法
    若p指向數組的某一個元素,則p++就指向其后續元素。
    3)指針的數組表示法
    若p=student,我們說指針p指向數組student,p[i]表示數組的第i個元素,其效果與student[i]等同。對數組成員的引用描述為:p[i].name、p[i].num等
    指向結構體數組的指針變量的使用

    [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;/*將數組的首地址賦值給指針p,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;??
  • }??
  • 執行結果如下:

    [cpp]?view plaincopy
  • fs@ubuntu:~/qiang/struct/tmp$?./struct2??
  • Outputname,number,year,month,day??
  • ??liying??????1???1978//5//23??
  • wangping??????2???1979//3//14??
  • ????libo??????3???1980//5//6??
  • ???xuyan??????4???1980//4//21??
  • fs@ubuntu:~/qiang/struct/tmp$???

  • ?

    附:給大家看一個有意思的程序:

    寫出一個模擬時鐘程序

    分析:我們知道時間有時 分 秒 組成,這里用結構體表示

    代碼如下:

    [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);//%02d中0?輸出數值時指定左面不使用的空位置自動填0,達到00:00:00效果??
  • ????fflush(stdout);//前面曾經講過,printf屬于行緩沖,遇到\n或程序結束才會輸出,這里沒有\n,所以用fflush刷新;??
  • }??
  • ??
  • 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;??
  • }??
  • 執行結果如下:

    [cpp]?view plaincopy
  • fs@ubuntu:~/qiang/struct$?./clock??
  • 00:00:01??
  • [cpp]?view plaincopy
  • fs@ubuntu:~/qiang/struct$?./clock??
  • 00:00:55??
  • 這里是個動態效果,大家可以打印出來看一下

    總結

    以上是生活随笔為你收集整理的Linux C 深入分析结构体指针的定义与引用的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。