結構體指針,C語言結構體指針詳解


結構體指針,可細分為指向結構體變量的指針和指向結構體數組的指針。

指向結構體變量的指針

前面我們通過“結構體變量名.成員名”的方式引用結構體變量中的成員,除了這種方法之外還可以使用指針。

前面講過,&student1 表示結構體變量 student1 的首地址,即 student1 第一個項的地址。如果定義一個指針變量 p 指向這個地址的話,p 就可以指向結構體變量 student1 中的任意一個成員。

那么,這個指針變量定義成什么類型呢?只能定義成結構體類型,且指向什么結構體類型的結構體變量,就要定義成什么樣的結構體類型。比如指向 struct STUDENT 類型的結構體變量,那么指針變量就一定要定義成 struct STUDENT* 類型。

下面將前面的程序用指針的方式修改一下:

# include <stdio.h>
# include <string.h>
struct AGE
{
    int year;
    int month;
    int day;
};
struct STUDENT
{
    char name[20];  //姓名
    int num;  //學號
    struct AGE birthday;  //生日
    float score;  //分數
};
int main(void)
{
    struct STUDENT student1; /*用struct STUDENT結構體類型定義結構體變量student1*/
    struct STUDENT *p = NULL;  /*定義一個指向struct STUDENT結構體類型的指針變量p*/
    p = &student1;  /*p指向結構體變量student1的首地址, 即第一個成員的地址*/
    strcpy((*p).name, "小明");  //(*p).name等價於student1.name
    (*p).birthday.year = 1989;
    (*p).birthday.month = 3;
    (*p).birthday.day = 29;
    (*p).num = 1207041;
    (*p).score = 100;
    printf("name : %s\n", (*p).name);  //(*p).name不能寫成p
    printf("birthday : %d-%d-%d\n", (*p).birthday.year, (*p).birthday.month, (*p).birthday.day);
    printf("num : %d\n", (*p).num);
    printf("score : %.1f\n", (*p).score);
    return 0;
}

輸出結果是:
name : 小明
birthday : 1989-3-29
num : 1207041
score : 100.0

我們看到,用指針引用結構體變量成員的方式是:

(*指針變量名).成員名

注意,*p 兩邊的括號不可省略,因為成員運算符“.”的優先級高於指針運算符“*”,所以如果 *p 兩邊的括號省略的話,那么 *p.num 就等價於 *(p.num) 了。

從該程序也可以看出:因為指針變量 p 指向的是結構體變量 student1 第一個成員的地址,即字符數組 name 的首地址,所以 p 和 (*p).name 是等價的。

但是,“等價”僅僅是說它們表示的是同一個內存單元的地址,但它們的類型是不同的。指針變量 p 是 struct STUDENT* 型的,而 (*p).name 是 char* 型的。所以在 strcpy 中不能將 (*p).name 改成 p。用 %s 進行輸入或輸出時,輸入參數或輸出參數也只能寫成 (*p).name 而不能寫成 p。

同樣,雖然 &student1 和 student1.name 表示的是同一個內存單元的地址,但它們的類型是不同的。&student1 是 struct STUDENT* 型的,而 student1.name 是 char* 型的,所以在對 p 進行初始化時,“p=&student1;”不能寫成“p=student1.name”。因為 p 是 struct STUDENT* 型的,所以不能將 char* 型的 student1.name 賦給 p。

此外為了使用的方便和直觀,用指針引用結構體變量成員的方式:

(*指針變量名).成員名

可以直接用:

指針變量名->成員名

來代替,它們是等價的。“->”是“指向結構體成員運算符”,它的優先級同結構體成員運算符“.”一樣高。p->num 的含義是:指針變量 p 所指向的結構體變量中的 num 成員。p->num 最終代表的就是 num 這個成員中的內容。

下面再將程序用“->”修改一下:

# include <stdio.h>
# include <string.h>
struct AGE
{
    int year;
    int month;
    int day;
};
struct STUDENT
{
    char name[20];  //姓名
    int num;  //學號
    struct AGE birthday;  /*用struct AGE結構體類型定義結構體變量birthday, 生日*/
    float score;  //分數
};
int main(void)
{
    struct STUDENT student1; /*用struct STUDENT結構體類型定義結構體變量student1*/
    struct STUDENT *p = NULL;  /*定義struct STUDENT結構體類型的指針變量p*/
    p = &student1;  /*p指向結構體變量student1的首地址, 即第一項的地址*/
    strcpy(p->name, "小明");
    p->birthday.year = 1989;
    p->birthday.month = 3;
    p->birthday.day = 29;
    p->num = 1207041;
    p->score = 100;
    printf("name : %s\n", p->name);  //p->name不能寫成p
    printf("birthday : %d-%d-%d\n", p->birthday.year, p->birthday.month, p->birthday.day);
    printf("num : %d\n", p->num);
    printf("score : %.1f\n", p->score);
    return 0;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM