1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
|
<title>STRUCT keyword </title>
<head>
<script language="JavaScript">
</script>
</head>
<body bgcolor="#ffffcc">
<hr>
<center><h1>STRUCT keyword </h1></center>
<hr>
<p>
<ul>
<li><a href="#basics">Structure basics.</a>
<li><a href="#membership">Structure membership.</a>
<li><a href="#pointers">Pointers to structures.</a>
<li><a href="#array">Array of structures.</a>
<li><a href="#geek">The bottom draw..</a>
<li><a href="../../CPLUSPLUS/SYNTAX/struct.html">C++ extensions</a>
</ul>
<a name=basics>
<hr>
<h2>Structure basics</h2>
<b>struct</b> is used to declare a new
<a href="../CONCEPT/data_types.html">data-type</a>.
Basically this means
grouping variables together. For example, a struct data type could be used
to declare the format of the following file.<p>
<center>
<table border=1 bgcolor="ivory">
<tr><td>
<pre>
Jo Loss Maths A
Harry Carpenter English A
Billy King Maths C
</pre>
</td></tr>
</table>
</center>
<p>
The records above could be described in a struct as follows:
<p>
<center>
<table border=1 bgcolor="ivory">
<tr><td>
<pre>
struct
{
char cname[8];
char sname[16];
char exam[16];
char grade;
} record;
</pre>
</td></tr>
</table>
</center>
<p>
The statement above declares a variable called <b>record</b> with 4 members
called <b>cname, sname, exam, grade</b>. The structure as a whole can be
referred to as <b>record</b> and a member can be referenced as
<b>record.exam</b>
<p>
Structures can be declared in various forms...
<p>
<pre>
struct x {int a; int b; int c;}; /* declaration */
struct {int a; int b; int c;} z;
struct x z;
</pre>
All the examples above are structure declarations,
<ul>
<li>The first gives <b>x</b> as a 'structure tag' - this is optional.
<li>The first and second declare the members of the structure.
<li>Second and third give <b>z</b> this is the variable that assumes the
structure type.
</ul>
<p>
<a name=membership>
<hr>
<h2>Structure membership</h2>
We can access individual members of a structure with the . operator.<p>
For example to assign a value, we can enter:
<p>
<center>
<table border=1 bgcolor="ivory">
<tr><td>
<pre>
struct x {int a; int b; int c;};
main()
{
struct x z;
z.a = 10;
z.b = 20;
z.c = 30;
}
</pre>
</td></tr>
</table>
</center>
<p>
And to retrieve a value from a structure member:
<p>
<center>
<table border=1 bgcolor="ivory">
<tr><td>
<pre>
struct x
{
int a;
int b;
int c;
};
main()
{
struct x z;
z.a = 10;
z.a++;
printf(" first member is %d \n", z.a);
}
</pre>
</td></tr>
</table>
</center>
<p>
<a name=pointers>
<hr>
<h2>Pointers to structures</h2>
<a href="#arrow">Fast path to an explanation of the -> operator.</a>
<p>
All that we have discussed so far has been OK but runs into problems
when structures have to be moved between functions for the following
reasons.
<ul>
<li>If the structure is large it is more effiecent to pass a
<a href="../CONCEPT/pointers.html">pointer</a> to the structure
instead of the structure its self. This technic is also used to pass
<a href="../CONCEPT/pointers.html#arrays">pointers to arrays</a> between
<a href="functions.html#2.5">functions.</a>
<p>
<li>When passing a structure to a function, you actually pass a COPY of the
structure. Therefore it is not possible to change the values of members
within the structure as the copy is destroyed when the function ends.
</ul>
<p>
So how does it all work? Here is an example. (make your browser W-I-D-E so
you can see the two examples).
<p>
<center>
<table border=1 bgcolor="ivory">
<tr><td>
<pre>
|
|
struct x {int a; int b; int c;} ; | struct x {int a; int b; int c;} ;
|
void function(struct x); | void function(struct x *);
|
main() | main()
{ | {
struct x z; | struct x z, *pz; <font color=red>/* 3 */</font>
| pz = &z; <font color=red>/* 4 */</font>
z.a = 10; <font color=red>/* 1 */</font> | z.a = 10;
z.a++; | z.a++;
|
function(z); <font color=red>/* 2 */</font> | function(pz); <font color=red>/* 5 */</font>
} | }
|
void function( struct x z) | void function(struct x * pz)
{ | { <font color=red>/* 6 */</font>
printf(" first member %d \n", z.a);| printf(" first member %d \n", (*pz).a);
} | }
|
</pre>
</td></tr>
</table>
</center>
<p>
Here is the annotation.
<ol>
<li>Give a structure member a value.
<li>Pass a COPY of the whole structure to the function.
<li>Define 'pz' a pointer to a structure of type 'x'.
<li>Put the address of 'z' into 'pz'. 'pz' now POINTS to 'z'.
PLEASE NOTE. 'z' is defined to reserve memory equal to the size of the
structure. 'pz' only holds an address so will be 4 bytes long.
<li>Pass the pointer into the function.
<li>Print the value of the member 'a'.
</ol>
<a name=arrow>
The <b>(*pz).a</b> syntax is used a great deal in C and it was decided to create
a short hand for it. So:
<pre>
(*pz).a == pz->a
</pre>
Here is the final picture.
<pre>
/*************************************************************************/
struct x {int a; int b; int c;} ; /* Declare the structure. */
void function(struct x * ); /* Declare the function. */
/*************************************************************************/
main()
{
/* Declare two variables.
* z == type struct x
* pz == a pointer to type struct x
*/
struct x z, *pz;
pz = &z; /* put the address of 'z' into 'pz' */
z.a = 10; /* initialize z.a */
z.a++; /* Increment z.a */
/* print the contents of 'z.a'
* using the pointer 'pz' */
printf(" first member before the function call %d \n", pz->a);
/* Call 'function' passing the
* pointer 'pz' */
function(pz);
/* Print the NEW value of 'z.a'
* using three different notations */
printf(" first member after the function call %d \n", pz->a);
printf(" first member after the function call %d \n", (*pz).a);
printf(" first member after the function call %d \n", z.a);
}
/*************************************************************************/
void function(struct x * pz)
{
/* Print the value of 'z.a' by
* referencing the pointer 'pz'
* which holds the address of 'z' */
printf(" first member inside the function %d \n", pz->a);
/* Increment the value of 'z.a'
* this is the source location
* in memory. */
pz->a++;
}
/*************************************************************************/
</pre>
<a name=geek>
<p>
<hr>
<h2>The Bottom Draw</h2>
Finally, here is a little feature that allows you to save a little
space.
<p>
<center>
<table border=1 bgcolor="ivory">
<tr><td>
<pre>
main()
{
struct Flags
{
unsigned int Online <font color=red>:1</font>;
unsigned int Mounted <font color=red>:1</font>;
}
struct Flags TapeInfo;
TapeInfo.Online = 1;
TapeInfo.Mounted = 0;
}
</pre>
</td></tr>
</table>
</center>
<p>
The <font color=red>:1</font> tells the compiler that only 1 byte is
required for <b>Online</b> and <b>Mounted</b>. There are a few points to
note about this though.
<ul>
<li>You may expect the compiler to reserve 2 bytes for the structure, it
actually reserves one word (usually 4 bytes) as this is the smallest unit
that can be reserved, the remaining 2 bytes are unavailable.
This is still better than the 2 words that wold normally get reserved.
<li>You can put any number into the variable, if the number is too large
to fit, the high order bits are lost without warning.
<li>Only <b>signed int</b>, <b>unsigned int</b>, <b>int</b> support this syntax.
</ul>
<hr>
<h2>Examples</h2>
<p>
<img src="../../GRAPHICS/computer.gif">
<a href="../EXAMPLES/struct1.c">
This is the most basic <b>struct</b> example I could think of.</a>
<br>
<img src="../../GRAPHICS/computer.gif">
<a href="../EXAMPLES/struct2.c">
Using structure elements, and passing them into a function.</a>
<br>
<img src="../../GRAPHICS/computer.gif">
<a href="../EXAMPLES/struct3.c">
Passing a whole structure to a function.</a> This performs a copy of the
structure so the same rules apply as for <b>int</b> etc.
Pointers to structures can be passed but I have not got to them yet....
<br>
<a name=array>
<img src="../../GRAPHICS/computer.gif">
<a href="../EXAMPLES/struct4.c">Define and use an array of structures.</a>
<br>
<img src=../../GRAPHICS/help.gif>
<a href="../PROBLEMS/problems.html#struct">
Here is a <b>struct</b> problem for you.</a>
<hr>
<h2>See Also:</h2>
<img src=../../GRAPHICS/whiteball.gif>
<a href="typedef.html">typedef</a> keyword.
<br>
<img src=../../GRAPHICS/whiteball.gif>
<a href="../MISC/linklists.html">Linked lists</a>.
<br>
<p>
<hr>
<p>
<center>
<table border=2 width="80%" bgcolor="ivory">
<tr align=center>
<td width="25%">
<a href="../cref.html"> Top</a>
</td><td width="25%">
<a href="../master_index.html"> Master Index</a>
</td><td width="25%">
<a href="keywords.html"> Keywords</a>
</td><td width="25%">
<a href="../FUNCTIONS/funcref.htm"> Functions</a>
</td>
</tr>
</table>
</center>
<p>
<hr>
<address>Martin Leslie
</address><p>
</body>
</html>
|