Linux C 使用指標訪問成員

2021-05-22 07:12:09 字數 511 閱讀 2095

在linux用c程式設計,很多時候都會碰到結構體這個概念,尤其是使用指標訪問結構體成員。(下面的文字介紹,請參考**理解)

1. 使用乙個新運算子:->,這個運算子有乙個連線號(-)後跟乙個大於符號(>)組成

struct guy *him;

him = &fellow[0];

him->income

2. 如果 him = &fellow[0] ,那麼 *him = fellow[0] ,   &和*是一對互逆的運算子,因此可做以下代替:

fellow[0].income  == (*him).income

必須有圓括號,因為,運算子比*的優先順序更高。

總之,後面跟->運算子的結構指標和後跟 . (點)運算子的結構名是一樣的,如果him是指向名為barney的guy型別結構的指標,則下列表示式是等價的:

barney.income  == (*him).income  == him->income     // 假設 him = &barney

**:

結構體指標訪問成員

結構體指標訪問成員 include struct student int main p stu1 printf 學號 t姓名 t分數 n printf d t s t 0.1f n p num,p name,p score 2.該種方法是定義乙個指標變數,只指向乙個struct student的結構...

this指標訪問成員函式問題

class cnullpointcall int cnullpointcall m istatic 0 void cnullpointcall test1 void cnullpointcall test2 void cnullpointcall test3 intitest void cnullp...

C 空指標訪問成員函式

空指標訪問成員函式 class person void showage int m age void test01 可以呼叫show,不能呼叫showage。呼叫show時,編譯器隱式加上了void show person this 雖然此時this p 為空,但是下面的函式裡沒有用到this,所以...