* remove "\r" nonsense
[mascara-docs.git] / C / the.ansi.c.programming.language / c.programming.notes.int / sx1c.html
blob712c892eab50b21ed93b0b4b23609f15d94fcf97
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.3: Operations on Structures</title>
10 <link href="sx1b.html" rev=precedes>
11 <link href="sx1d.html" rel=precedes>
12 <link href="sx1.html" rev=subdocument>
13 </head>
14 <body>
15 <H2>15.3: Operations on Structures</H2>
17 <p>[This section corresponds roughly to K&amp;R Sec. 6.2]
18 </p><p>There is a relatively small number of operations
19 which C directly supports on structures.
20 As we've seen, we can define structures,
21 declare variables of structure type,
22 and select the members of structures.
23 We can also assign entire structures:
24 the expression
25 <pre>
26 c1 = c2
27 </pre>
28 would assign <em>all</em> of <TT>c2</TT> to <TT>c1</TT>
29 (both the real and imaginary parts,
30 assuming the preceding declarations).
31 We can also pass structures as arguments to functions,
32 and declare and define functions which return structures.
33 But to do anything else,
34 we typically have to write our own code
35 (often as functions).
36 For example, we could write a function to add two complex numbers:
37 <pre>
38 struct complex
39 cpx_add(struct complex c1, struct complex c2)
41 struct complex sum;
42 sum.real = c1.real + c2.real;
43 sum.imag = c1.imag + c2.imag;
44 return sum;
46 </pre>
47 We could then say things like
48 <pre>
49 c1 = cpx_add(c2, c3)
50 </pre>
51 (Two footnotes:
52 Typically,
53 you do need a temporary variable like <TT>sum</TT>
54 in a function like <TT>cpx_add</TT> above that returns a structure.
56 In C++,
57 it's possible to couple your own functions to the built-in operators,
58 so that you could write <TT>c1 = c2 + c2</TT>
59 and have it call your complex add function.)
60 </p><p>One more thing you can do with a structure is
61 initialize a structure variable while declaring it.
62 As for array initializations,
64 the initializer consists of
65 a comma-separated list of values enclosed in braces <TT>{}</TT>:
66 <pre>
67 struct complex c1 = {1, 2};
68 struct complex c2 = {3, 4};
69 </pre>
70 Of course,
71 the type of each initializer in the list
72 must be compatible with the type of the corresponding structure member.
73 </p><hr>
74 <p>
75 Read sequentially:
76 <a href="sx1b.html" rev=precedes>prev</a>
77 <a href="sx1d.html" rel=precedes>next</a>
78 <a href="sx1.html" rev=subdocument>up</a>
79 <a href="top.html">top</a>
80 </p>
81 <p>
82 This page by <a href="http://www.eskimo.com/~scs/">Steve Summit</a>
83 // <a href="copyright.html">Copyright</a> 1996-1999
84 // <a href="mailto:scs@eskimo.com">mail feedback</a>
85 </p>
86 </body>
87 </html>