Skip to content

Latest commit

 

History

History
121 lines (95 loc) · 3.26 KB

File metadata and controls

121 lines (95 loc) · 3.26 KB
title ms.custom ms.date ms.prod ms.reviewer ms.suite ms.technology ms.tgt_pltfrm ms.topic dev_langs helpviewer_keywords ms.assetid caps.latest.revision ms.author manager translation.priority.ht
Structure and Union Members
na
10/14/2016
visual-studio-dev14
na
na
devlang-cpp
na
article
C++
C
member-selection operators
structure members, referencing
-> operator, structure and union members
dot operator (.)
referencing structure members
. operator
operators [C], member selection
structure member selection
bb1fe304-af49-4f98-808d-afdc99b3e319
8
mithom
ghogen
cs-cz
de-de
es-es
fr-fr
it-it
ja-jp
ko-kr
pl-pl
pt-br
ru-ru
tr-tr
zh-cn
zh-tw

Structure and Union Members

A "member-selection expression" refers to members of structures and unions. Such an expression has the value and type of the selected member.

  
postfix-expression  
.  
identifier  
postfix-expression  
–>  
identifier  
  

This list describes the two forms of the member-selection expressions:

  1. In the first form, postfix-expression represents a value of struct or union type, and identifier names a member of the specified structure or union. The value of the operation is that of identifier and is an l-value if postfix-expression is an l-value. See L-Value and R-Value Expressions for more information.

  2. In the second form, postfix-expression represents a pointer to a structure or union, and identifier names a member of the specified structure or union. The value is that of identifier and is an l-value.

The two forms of member-selection expressions have similar effects.

In fact, an expression involving the member-selection operator (–>) is a shorthand version of an expression using the period (.) if the expression before the period consists of the indirection operator (*) applied to a pointer value. Therefore,

  
expression  
–>  
identifier  
  

is equivalent to

  
(*  
expression  
) .  
identifier  
  

when expression is a pointer value.

Examples

The following examples refer to this structure declaration. For information about the indirection operator (*) used in these examples, see Indirection and Address-of Operators.

struct pair   
{  
    int a;  
    int b;  
    struct pair *sp;  
} item, list[10];  

A member-selection expression for the item structure looks like this:

item.sp = &item;  

In the example above, the address of the item structure is assigned to the sp member of the structure. This means that item contains a pointer to itself.

(item.sp)–>a = 24;  

In this example, the pointer expression item.sp is used with the member-selection operator (–>) to assign a value to the member a.

list[8].b = 12;  

This statement shows how to select an individual structure member from an array of structures.

See Also

Member Access Operators: . and ->