* remove "\r" nonsense
[mascara-docs.git] / C / the.ansi.c.programming.language / c.programming.notes.int / sx1b.html
blob7ceddec686981e0519f026189ce39298832f5b18
1 <!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN">
2 <!-- This collection of hypertext pages is Copyright 1995-7 by Steve Summit. -->
3 <!-- This material may be freely redistributed and used -->
4 <!-- but may not be republished or sold without permission. -->
5 <html>
6 <head>
7 <link rev="owner" href="mailto:scs@eskimo.com">
8 <link rev="made" href="mailto:scs@eskimo.com">
9 <title>15.2: Accessing Members of Structures</title>
10 <link href="sx1a.html" rev=precedes>
11 <link href="sx1c.html" rel=precedes>
12 <link href="sx1.html" rev=subdocument>
13 </head>
14 <body>
15 <H2>15.2: Accessing Members of Structures</H2>
17 <p>We said that a structure was a little bit like an array:
18 a collection of members (elements).
19 We access the elements of an array
20 by using a numeric subscript in square brackets <TT>[]</TT>.
21 We access the elements of a structure
22 by name,
23 using the
24 <dfn>structure selection operator</dfn> which is a dot (a period).
25 The structure selection operator is a little like
26 the other binary operators we've seen,
27 but much more restricted:
28 on its left must be a variable or object of structure type,
29 and on its right must be the name of one of the members of that structure.
30 For example,
31 if <TT>c1</TT> is a variable of type <TT>struct complex</TT>
32 as declared in the previous section,
33 then <TT>c1.real</TT> is its real part
34 and <TT>c1.imag</TT> is its imaginary part.
35 </p><p>Like subscripted array references,
36 references to the members of structure variables
37 (using the structure selection operator)
38 can appear anywhere,
39 either on the right or left side of assignment operators.
40 We could say
41 <pre>
42 c1.real = 1
43 </pre>
44 to set the real part of <TT>c1</TT>
45 (that is, the <TT>real</TT> member within <TT>c1</TT>)
46 to 1,
48 <pre>
49 c1.imag = c2.imag
50 </pre>
51 to fetch the imaginary part of <TT>c2</TT> and assign it to
52 the imaginary part of <TT>c1</TT>,
54 <pre>
55 c1.real = c2.real + c3.real
56 </pre>
57 to take the real parts of <TT>c2</TT> and <TT>c3</TT>,
58 add them together,
59 and assign the result to the real part of <TT>c1</TT>.
60 </p><hr>
61 <p>
62 Read sequentially:
63 <a href="sx1a.html" rev=precedes>prev</a>
64 <a href="sx1c.html" rel=precedes>next</a>
65 <a href="sx1.html" rev=subdocument>up</a>
66 <a href="top.html">top</a>
67 </p>
68 <p>
69 This page by <a href="http://www.eskimo.com/~scs/">Steve Summit</a>
70 // <a href="copyright.html">Copyright</a> 1996-1999
71 // <a href="mailto:scs@eskimo.com">mail feedback</a>
72 </p>
73 </body>
74 </html>