User-defined data types: the C struct construct

  • User defined data types are defined using the struct construct

  • Syntax to define a struct (user defined) data type in C:

        struct structureName {
           variable definitions;
        } [varname1, varName2] ;  // Optional elements 
      

  • Example:

        struct BankAccount {                         
           int   ID;
           float balance;
        } ;         
      

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

Comparing C's struct with Java's class construct

  • The struct construct in C is similar to a class definition in Java that specifies the instance variables:

      C's struct Java's class
       struct BankAccount      
       { 
          int   ID;
          float balance;
       }
      
       public class BankAccount    
       {
          public int   ID;
          public float balance;
       }
      

  • Differences:

      1. A struct does not contain any (member) function definitions

      2. No access protection in a C's struct

      3. C provides static struct variables while Java always create "dynamic" objects (using the new operator)

Defining (user-defined) struct typed variables

  • You can define struct typed variable(s) after defining the struct data type definition

  • 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 
      

C creates static struct variables

How the C compiler processes a struct variable definition:

  struct BankAccount { 
     int   ID;
     float balance;
  } ;  

  struct BankAccount acc1; // <--- How does the C 
                           // compiler translate this ?

Output generated by the C compiler:

  .data

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

You can see that the acc1 "object" is inmobile (= static) due to the label acc1 used in the variable definition

Quicker struct typed 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 typed 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 typed variable

Example: using a struct typed variable

 #include <stdio.h>

 // You must define the struct definition first
 struct BankAccount {   
   int   ID;
   float balance;
 };

 // Afterwards, you can define struct typed variables
 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 are usually stored 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 data type definition