?
Ce document utilise Manuel du site Web PHP chinois Libérer
成員訪問(wèn)操作符允許訪問(wèn)其操作數(shù)的成員。
Operator | Operator name | Example | Description |
---|---|---|---|
[] | array subscript | ab | access the bth element of array a |
* | pointer dereference | *a | dereference the pointer a to access the object or function it refers to |
& | address of | &a | create a pointer that refers to the object or function a |
. | member access | a.b | access member b of struct or union a |
-> | member access through pointer | a->b | access member b of struct or union pointed to by a |
數(shù)組subscrpt表達(dá)式的形式。
pointer-expression integer-expression | (1) | |
---|---|---|
integer-expression pointer-expression | (2) |
其中
指針表達(dá) | - | 指向完整對(duì)象的類(lèi)型指針表達(dá)式 |
---|---|---|
整數(shù)表達(dá)式 | - | 整數(shù)類(lèi)型的表達(dá)式 |
下標(biāo)運(yùn)算符表達(dá)式是lvalue表達(dá)式,其類(lèi)型是指針表達(dá)式指向的對(duì)象的類(lèi)型。
根據(jù)定義,下標(biāo)運(yùn)算符E1[E2]
與*((E1)+(E2))
。完全相同。如果pointer-expression是一個(gè)數(shù)組表達(dá)式,它將進(jìn)行左值到右值的轉(zhuǎn)換,并成為指向數(shù)組的第一個(gè)元素的指針。
由于指針和整數(shù)之間的加法定義,結(jié)果是數(shù)組的元素,其索引等于integer-expression的結(jié)果(或者,如果指針表達(dá)式指向某個(gè)數(shù)組的第i個(gè)元素,則結(jié)果是我加上整數(shù)表達(dá)式的結(jié)果)。
注意:有關(guān)多維數(shù)組的詳細(xì)信息,請(qǐng)參閱數(shù)組。
#include <stdio.h>int main(void){ int a[3] = {1,2,3}; printf("%d %d\n", a[2], // n == 3 2[a]); // same, n == 3 a[2] = 7; // subscripts are lvalues int n[2][3] = {1,2,3,4,5,6}; int (*p)[3] = &n[1]; // elements of n are arrays int x = n[1][2]; // applying [] again to the array n[1] printf("%c %c\n", "abc"[2], 2["abc"]); // string literals are arrays too}
輸出:
3 3c c
的解引用或間接表達(dá)式具有形式。
* pointer-expression |
---|
其中
pointer-expression | - | an expression of any pointer type |
---|
如果指針表達(dá)式是指向函數(shù)的指針,則解引用運(yùn)算符的結(jié)果是該函數(shù)的函數(shù)指示符。
如果指針表達(dá)式是一個(gè)指向?qū)ο蟮闹羔?,則結(jié)果是一個(gè)指定指向?qū)ο蟮淖笾当磉_(dá)式。
取消引用空指針,指向其生命期之外的對(duì)象的指針(懸掛指針),未對(duì)齊的指針或具有不確定值的指針是未定義的行為,除非通過(guò)將地址 - of運(yùn)算符應(yīng)用于其結(jié)果,如在&*E
。
#include <stdio.h>int main(void){ int n = 1; int* p = &n; printf("*p = %d\n", *p); // the value of *p is what's stored in n *p = 7; // *p is lvalue printf("*p = %d\n", *p);}
輸出:
*p = 1*p = 7
表達(dá)式的地址具有這種形式。
& function | (1) | |
---|---|---|
& lvalue-expression | (2) | |
& * expression | (3) | |
& expression expression | (4) |
1)功能的地址
2)對(duì)象的地址
3)特殊情況:&和*互相取消,沒(méi)有一個(gè)人被評(píng)估
4)特例:&和[]中隱含的*相互抵消,只評(píng)估[]中暗示的加法
其中
lvalue-expression | - | an lvalue expression of any type that is not a bit field and does not have register storage class |
---|
地址 - 運(yùn)算符產(chǎn)生其操作數(shù)的非左值地址,適合于初始化指向操作數(shù)類(lèi)型的指針。如果操作數(shù)是函數(shù)指示符((1)),則結(jié)果是指向函數(shù)的指針。如果操作數(shù)是一個(gè)對(duì)象((2)),則結(jié)果是一個(gè)指向?qū)ο蟮闹羔槨?/p>
如果操作數(shù)是取消引用操作符,則不采取任何操作(因此可將&應(yīng)用于空指針),除非結(jié)果不是左值。
如果操作數(shù)是一個(gè)數(shù)組索引表達(dá)式,則不采取任何動(dòng)作比陣列到指針轉(zhuǎn)換和添加其他,所以&AN是有效的大小為N的一個(gè)陣列(獲得一個(gè)指針一個(gè)過(guò)去的端部還行,解除引用它是不,但在這個(gè)表達(dá)式中取消引用)。
int f(char c) { return c;}int main(void){ int n = 1; int *p = &n; // address of object n int (*fp)(char) = &f; // address of function f int a[3] = {1,2,3}; int *beg=a, *end=&a[3]; // same as end = n+3}
成員訪問(wèn)表達(dá)式具有表單。
expression . member-name |
---|
其中
表達(dá) | - | 結(jié)構(gòu)或聯(lián)合類(lèi)型的表達(dá)式 |
---|---|---|
成員名字 | - | 一個(gè)標(biāo)識(shí)符,用于命名由expression指定的結(jié)構(gòu)體或聯(lián)合體的成員 |
成員訪問(wèn)表達(dá)式指定由其左操作數(shù)指定的結(jié)構(gòu)體或聯(lián)合體的已命名成員。它與左操作數(shù)具有相同的值類(lèi)別。
如果左操作數(shù)是const或volatile限定的,結(jié)果也是合格的。如果左操作數(shù)是原子的,則行為是未定義的。
注意:除了名稱(chēng)為struct或union類(lèi)型的對(duì)象的標(biāo)識(shí)符外,以下表達(dá)式可能具有struct或union類(lèi)型:賦值,函數(shù)調(diào)用,逗號(hào)運(yùn)算符,條件運(yùn)算符和復(fù)合文字。
#include <stdio.h>struct s {int x;};struct s f(void) { return (struct s){1}; }int main(void){ struct s s; s.x = 1; // ok, changes the member of s int n = f().x; // f() is an expression of type struct s// f().x = 1; // Error: this member access expression is not an lvalue const struct s sc;// sc.x = 3; // Error: sc.x is const, can't be assigned union { int x; double d; } u = {1}; u.d = 0.1; // changes the active member of the union}
成員訪問(wèn)表達(dá)式具有表單。
expression -> member-name |
---|
其中
表達(dá) | - | 指向結(jié)構(gòu)或聯(lián)合的類(lèi)型指針表達(dá)式 |
---|---|---|
成員名字 | - | 一個(gè)標(biāo)識(shí)符,用于命名表達(dá)式指向的結(jié)構(gòu)體或聯(lián)合體的成員 |
通過(guò)指針表達(dá)式的成員訪問(wèn)指定左操作數(shù)指向的struct或union類(lèi)型的已命名成員。其價(jià)值類(lèi)別始終是左值。
如果左操作數(shù)指向的類(lèi)型是const或volatile限定的,則結(jié)果也是合格的。如果左操作數(shù)指向的類(lèi)型是原子的,則行為是未定義的。
#include <stdio.h>struct s {int x;};int main(void){ struct s s={1}, *p = &s; p->x = 7; // changes the value of s.x through the pointer printf("%d\n", p->x); // prints 7}