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[8];
       char sname[16];   
       char exam[16];
       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,


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

Fast path to an explanation of the -> operator.

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.

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'.
The (*pz).a syntax is used a great deal in C and it was decided to create a short hand for it. So:
	(*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 only 1 byte is required for Online and Mounted. There are a few points to note about this though.


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.


See Also:

typedef keyword.
Linked lists.


Top Master Index Keywords Functions


Martin Leslie