Member access operators allow access to the members of their operands.
| Operator | Operator name | Example | Description |
|---|---|---|---|
| [] | array subscript | a[b] | access thebth element of arraya |
| * | pointer dereference | *a | dereference the pointera to access the object or function it refers to |
| & | address of | &a | create a pointer that refers to the object or functiona |
| . | member access | a.b | access memberb ofstruct oruniona |
| -> | member access through pointer | a->b | access memberb ofstruct orunion pointed to bya |
Contents |
The array subscript expression has the form
pointer-expression[integer-expression] | (1) | ||||||||
integer-expression[pointer-expression] | (2) | ||||||||
where
| pointer-expression | - | anexpression of type pointer to complete object |
| integer-expression | - | anexpression of integer type |
The subscript operator expression is anlvalue expression whose type is the type of the object pointed to bypointer-expression.
By definition, the subscript operatorE1[E2] is exactly identical to*((E1)+(E2)). Ifpointer-expression is an array expression, it undergoeslvalue-to-rvalue conversion and becomes a pointer to the first element of the array.
Due to the definition of theaddition between a pointer and an integer, the result is the element of the array with the index equal to the result ofinteger-expression (or, ifpointer-expression was pointing at ith element of some array, the index of the result is i plus the result ofinteger-expression)
Note: seearray for the details on multidimensional arrays.
#include <stdio.h>int main(void){int a[3]={1,2,3};printf("%d %d\n", a[2],// n == 32[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 arraysprintf("%d %d %d\n",(*p)[0], p[0][1], p[0][2]);// access n[1][] via pint x= n[1][2];// applying [] again to the array n[1]printf("%d\n", x); printf("%c %c\n","abc"[2],2["abc"]);// string literals are arrays too}
Output:
3 34 5 66c c
Thedereference orindirection expression has the form
*pointer-expression | |||||||||
where
| pointer-expression | - | anexpression of any pointer type |
Ifpointer-expression is a pointer to function, the result of the dereference operator is a function designator for that function.
Ifpointer-expression is a pointer to object, the result is anlvalue expression that designates the pointed-to object.
Dereferencing a null pointer, a pointer to an object outside of its lifetime (a dangling pointer), a misaligned pointer, or a pointer with indeterminate value is undefined behavior, except when the dereference operator is nullified by applying the address-of operator to its result, as in&*E.
Output:
*p = 1*p = 7
The address-of expression has the form
&function | (1) | ||||||||
&lvalue-expression | (2) | ||||||||
&*expression | (3) | ||||||||
&expression[expression] | (4) | ||||||||
& and* cancel each other, neither one is evaluated& and the* that is implied in[] cancel each other, only the addition implied in[] is evaluated.where
| lvalue-expression | - | anlvalue expression of any type that is not abit-field and does not haveregister storage class |
The address-of operator produces thenon-lvalue address of its operand, suitable for initializing a pointer to the type of the operand. If the operand is a function designator(1), the result is a pointer to function. If the operand is an object(2), the result is a pointer to object.
If the operand is the dereference operator, no action is taken (so it's okay to apply &* to a null pointer), except that the result is not an lvalue.
If the operand is an array index expression, no action is taken other than the array-to-pointer conversion and the addition, so &a[N] is valid for an array of size N (obtaining a pointer one past the end is okay, dereferencing it is not, but dereference cancels out in this expression).
int f(char c){return c;}int main(void){int n=1;int*p=&n;// address of object nint(*fp)(char)=&f;// address of function fint a[3]={1,2,3};int*beg=a,*end=&a[3];// same as end = a+3}
The member access expression has the form
expression.member-name | |||||||||
where
| expression | - | an expression ofstruct orunion type |
| member-name | - | anidentifier that names a member of the struct or union designated byexpression |
The member access expression designates the named member of thestruct orunion designated by its left operand. It has the samevalue category as its left operand.
If the left operand isconst orvolatile qualified, the result is also qualified. If the left operand isatomic, the behavior is undefined.
Note: besides identifiers that name objects of struct or union type, the following expressions may have struct or union types:assignment,function call,comma operator,conditional operator, andcompound literal.
#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 sint n= f().x;// f() is an expression of type struct s// f().x = 1; // Error: this member access expression is not an lvalue conststruct 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}
The member access expression has the form
expression->member-name | |||||||||
where
| expression | - | an expression of typepointer tostruct orunion |
| member-name | - | anidentifier that names a member of the struct or union pointed byexpression |
The member access through pointer expression designates the named member of thestruct orunion type pointed to by its left operand. Its value category is alwayslvalue
If the type pointed to by the left operand isconst orvolatile qualified, the result is also qualified. If the type pointed to by the left operand isatomic, the behavior is undefined.
#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 pointerprintf("%d\n", p->x);// prints 7}
The following behavior-changing defect reports were applied retroactively to previously published C standards.
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| DR 076 | C89 | unnessary indirection could not be cancelled by& | made cancellable |
| Common operators | ||||||
|---|---|---|---|---|---|---|
| assignment | increment decrement | arithmetic | logical | comparison | member access | other |
a= b | ++a | +a | !a | a== b | a[b] | a(...) |
C++ documentation forMember access operators |