diff options
Diffstat (limited to 'reference/C/SYNTAX/struct.html')
-rw-r--r-- | reference/C/SYNTAX/struct.html | 392 |
1 files changed, 392 insertions, 0 deletions
diff --git a/reference/C/SYNTAX/struct.html b/reference/C/SYNTAX/struct.html new file mode 100644 index 0000000..6465ee6 --- /dev/null +++ b/reference/C/SYNTAX/struct.html @@ -0,0 +1,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> |