Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      Member access operators

      From cppreference.com
      <c‎ |language
       
       
       
       

      Member access operators allow access to the members of their operands.

      Operator Operator name Example Description
      [] array subscripta[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 accessa.b access memberb ofstruct oruniona
      -> member access through pointera->b access memberb ofstruct orunion pointed to bya

      Contents

      [edit]Subscript

      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.

      Run this code
      #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

      [edit]Dereference

      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.

      Run this code
      #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 lvalueprintf("*p = %d\n",*p);}

      Output:

      *p = 1*p = 7

      [edit]Address of

      The address-of expression has the form

      &function (1)
      &lvalue-expression (2)
      &*expression (3)
      &expression[expression] (4)
      1) address of a function
      2) address of an object
      3) special case:& and* cancel each other, neither one is evaluated
      4) special case:& 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).

      Run this code
      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}

      [edit]Member access

      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.

      Run this code
      #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}

      [edit]Member access through pointer

      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.

      Run this code
      #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}

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C standards.

      DRApplied toBehavior as publishedCorrect behavior
      DR 076C89unnessary indirection could not be cancelled by&made cancellable

      [edit]References

      • C17 standard (ISO/IEC 9899:2018):
      • 6.5.2.1 Array subscripting (p: 57-58)
      • 6.5.2.3 Structure and union members (p: 58-59)
      • 6.5.3.2 Address and indirection operators (p: 59-61)
      • C11 standard (ISO/IEC 9899:2011):
      • 6.5.2.1 Array subscripting (p: 80)
      • 6.5.2.3 Structure and union members (p: 82-84)
      • 6.5.3.2 Address and indirection operators (p: 88-89)
      • C99 standard (ISO/IEC 9899:1999):
      • 6.5.2.1 Array subscripting (p: 70)
      • 6.5.2.3 Structure and union members (p: 72-74)
      • 6.5.3.2 Address and indirection operators (p: 78-79)
      • C89/C90 standard (ISO/IEC 9899:1990):
      • 3.3.2.1 Array subscripting
      • 3.3.2.3 Structure and union members
      • 3.3.3.2 Address and indirection operators

      [edit]See also

      Common operators
      assignmentincrement
      decrement
      arithmeticlogicalcomparisonmember
      access
      other

      a= b
      a+= b
      a-= b
      a*= b
      a/= b
      a%= b
      a&= b
      a|= b
      a^= b
      a<<= b
      a>>= b

      ++a
      --a
      a++
      a--

      +a
      -a
      a+ b
      a- b
      a* b
      a/ b
      a% b
      ~a
      a& b
      a| b
      a^ b
      a<< b
      a>> b

      !a
      a&& b
      a|| b

      a== b
      a!= b
      a< b
      a> b
      a<= b
      a>= b

      a[b]
      *a
      &a
      a->b
      a.b

      a(...)
      a, b
      (type) a
      a? b: c
      sizeof


      _Alignof
      (since C11)
      (until C23)

      alignof
      (since C23)

      C++ documentation forMember access operators
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/language/operator_member_access&oldid=150169"

      [8]ページ先頭

      ©2009-2026 Movatter.jp