The C struct construct
 

  • The struct construct in C is used to group related variables together

  • The struct construct in C is similar to a class definition in Java

    And a struct variable in C is comparable to an object in Java.

    Except that:

      • A struct does not contain any (member) function definitions

        (A struct only contain variable definitions)

      • There are no access specifiers (like private, protected, etc)

      • C has "static" struct variables while Java has "dynamic" objects (created using the new operator)

Defining a struct data type in C

  • Syntax to define a struct data type in C:

        struct structName {
           variable definitions;
        } [varname1, varName2] ;            
      

    Example:

        struct BankAccount {       
           int   ID;
           float balance;
        } ;                  
      

  • struct data type definitions must be placed outside a function !

Note: things inside [ .... ] are optional elements

Defining a struct typed variable
 

  • Syntax to define a struct typed variable in C:

        struct structName  varName ;        
      

    Example:

        struct BankAccount {  // Must define struct first   
           int   ID;
           float balance;
        } ;  
      
        struct BankAccount john, mary; // Then define variable 
      

Comment: the struct variable definitions will allocate memory to store all fields in the struct data type !

How does the C compiler process a struct definition
 

  struct BankAccount { 
     int   ID;
     float balance;
  } ;  

  struct BankAccount john, mary;  

will cause the C compiler to output:

  .data

  john:  .skip 8 // 4 bytes to store an int +
                 // 4 bytes to store a float
         
  mary:  .skip 8  

You can see that the objects are inmobile (= static) due to the labels used.

Quick struct variable definition
 

  • You may include variable definitions when you define a struct:

        struct BankAccount {   
           int   ID;
           float balance;
        } john ;  // Defines a "struct" variable 
                  // along with the "struct" data type
      
        struct BankAccount mary; // Additional variables 
                                 // can be define later 
      

Using a struct variable
 

  • A struct variable in C is like an object in Java

  • Just like in Java, you can only access a specific field in a struct variable

  • Syntax to access a field in a struct variable x

          x.fieldName                    
      

Using a struct variable

Example: using a struct typed variable

 #include <stdio.h>

 struct BankAccount {   
   int   ID;
   float balance;
 };

 struct BankAccount mary; // Global struct variable

 int main( int argc, char *argv[] )
 {
   struct BankAccount john; // Local struct variable

   mary.balance = 900;
   john.balance = 500;
   printf("%f\n\n", mary.balance + john.balance);
 } 

 

struct typed parameters in functions

How to specific a struct parameter in a function:

 #include <stdio.h>

 struct BankAccount {   
   int   ID;
   float balance;
 };

 void printBal( struct BankAccount x )
 {
    printf("%f\n", x.balance);
 }

 int main(int argc, char *argv[])
 {
   struct BankAccount john; // Local struct variable

   john.balance = 500;
   printBal( john );   // Pass a struct variable as parameter
 }  

struct typed variables are passed-by-value in C

Evidence that struct typed variables are passed-by-value:

 #include <stdio.h>

 struct BankAccount {   
   int   ID;
   float balance;
 };

 void doubleBal( struct BankAccount x )
 {
    x.balance = 2*x.balance;  // Update parameter
 }

 int main(int argc, char *argv[])
 {
   struct BankAccount john; // Local struct variable

   john.balance = 500;
   doubleBal( john );
   printf("%f\n", john.balance); //Prints 500 - unchanged!!
 }   

How to declare a function with struct typed parameter

Situation where we need to declare a function:   calls a function before its definition

 #include <stdio.h>

 struct BankAccount {   
   int   ID;
   float balance;
 };



 int main(int argc, char *argv[])
 {
   struct BankAccount john; // Local struct variable

   john.balance = 500;
   doubleBal( john );  // Uses function before definition  
   printf("%f\n", john.balance); 
 }  
 
 void doubleBal( struct BankAccount x )
 {
    x.balance = 2*x.balance;
 }

How to declare a function with struct typed parameter

How to declare a function with struct typed as parameter:

 #include <stdio.h>

 struct BankAccount {   
   int   ID;
   float balance;
 };

 void doubleBal( struct BankAccount x );

 int main(int argc, char *argv[])
 {
   struct BankAccount john; // Local struct variable

   john.balance = 500;
   doubleBal( john );  // Compiler can check data type now
   printf("%f\n", john.balance); 
 }  
 
 void doubleBal( struct BankAccount x )
 {
    x.balance = 2*x.balance;
 } 

typedef
 

  • A (user defined) struct type (and other data types) can be given a type name (= "alias") using the typedef keyword

  • Syntax:

         typedef  dataType  alias ;    
      
       Example:
      
         typedef  struct BankAccount  BankAccount_t ; 
      

    You can now use BankAccount_t in the place of struct BankAccount anywhere in the program.

Example of using typedef

 #include <stdio.h>

 struct BankAccount {   
   int   ID;
   float balance;
 };

 typedef struct BankAccount BankAccount_t;

 void doubleBal( BankAccount_t x )
 {
    x.balance = 2*x.balance;
 }

 int main(int argc, char *argv[])
 {
   BankAccount_t john; // Local struct variable

   john.balance = 500;
   doubleBal( john );
   printf("%f\n", john.balance); // Prints 500 - unchanged !!
 }    

struct definitions and typedef definitions should be in a header file
 

Example header file

 // Filename: bankaccount.h
 // Sample header file for 
 // the BankAccount data type definition

 struct BankAccount {   
   int   ID;
   float balance;
 };

 typedef  struct BankAccount BankAccount_t;
 

C programs will use #include "bankaccount.h" at the top of the program file to obtain the struct BankAccount definition