# STRUCT keyword

## Structure basics

struct is used to declare a new data-type. Basically this means grouping variables together. For example, a struct data type could be used to declare the format of the following file.

 ``` Jo Loss Maths A Harry Carpenter English A Billy King Maths C ```

The records above could be described in a struct as follows:

 ``` struct { char cname; char sname; char exam; char grade; } record; ```

The statement above declares a variable called record with 4 members called cname, sname, exam, grade. The structure as a whole can be referred to as record and a member can be referenced as record.exam

Structures can be declared in various forms...

```	struct x {int a; int b; int c;};		/* declaration	*/
struct 	 {int a; int b; int c;} z;
struct x z;
```

All the examples above are structure declarations,

• The first gives x as a 'structure tag' - this is optional.
• The first and second declare the members of the structure.
• Second and third give z this is the variable that assumes the structure type.

## Structure membership

We can access individual members of a structure with the . operator.

For example to assign a value, we can enter:

 ``` struct x {int a; int b; int c;}; main() { struct x z; z.a = 10; z.b = 20; z.c = 30; } ```

And to retrieve a value from a structure member:

 ``` 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); } ```

## Pointers to structures

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.

• If the structure is large it is more effiecent to pass a pointer to the structure instead of the structure its self. This technic is also used to pass pointers to arrays between functions.

• 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.

So how does it all work? Here is an example. (make your browser W-I-D-E so you can see the two examples).

 ``` | | 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; /* 3 */ | pz = &z; /* 4 */ z.a = 10; /* 1 */ | z.a = 10; z.a++; | z.a++; | function(z); /* 2 */ | function(pz); /* 5 */ } | } | void function( struct x z) | void function(struct x * pz) { | { /* 6 */ printf(" first member %d \n", z.a);| printf(" first member %d \n", (*pz).a); } | } | ```

Here is the annotation.

1. Give a structure member a value.
2. Pass a COPY of the whole structure to the function.
3. Define 'pz' a pointer to a structure of type 'x'.
4. 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.
5. Pass the pointer into the function.
6. Print the value of the member 'a'.
```	(*pz).a   ==  pz->a
```

Here is the final picture.

```
/*************************************************************************/

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++;

}

/*************************************************************************/

```

## The Bottom Draw

Finally, here is a little feature that allows you to save a little space.

 ``` main() { struct Flags { unsigned int Online :1; unsigned int Mounted :1; } struct Flags TapeInfo; TapeInfo.Online = 1; TapeInfo.Mounted = 0; } ```

The :1 tells the compiler that each is only 1 bit long an so only 1 byte is required for both Online and Mounted. There are a few points to note about this though.

• 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.
• You can put any number into the variable, if the number is too large to fit, the high order bits are lost without warning.
• Only signed int, unsigned int, int support this syntax.

## Examples This is the most basic struct example I could think of. Using structure elements, and passing them into a function. Passing a whole structure to a function. This performs a copy of the structure so the same rules apply as for int etc. Pointers to structures can be passed but I have not got to them yet.... Define and use an array of structures. Here is a struct problem for you. typedef keyword. Linked lists.