Declaring (global) struct variables

  • The C compiler will need the data type information of the fields in a struct data type to perform the translation correctly

  • Therefore:

      • Global struct variables must be declared if they are used before their definition

  • Syntax to declare a struct variable:

       // Note: the struct definition must      
       //       appear before the declaration ! 
       //       (to provide data type info)     
      
       extern  struct  StructName   varName ; 

Example how to declare a (global) struct variable

Suppose a global struct BankAccount variable a is defined in struct2a.c and used in struct2b.c:

struct2a.c struct2b.c
/* ---------------------------
   Define the structure FIRST
   ---------------------------- */
struct BankAccount
{
   int    ID;
   double balance;
};

/* ------------------------------------
   THEN you can define a struct variable
   ------------------------------------ */
struct BankAccount a;

int main(int argc, char *argv[])
{
   a.ID  = 123;
   a.balance = 1000.0;

   print(); // Invoke print() defined 
            // inside a different file   
}
/* --------------------------
   Define the structure FIRST
   -------------------------- */
struct BankAccount
{
   int    ID;
   double balance;
};

/* ----------------------------
   THEN you can declare variable
   ---------------------------- */
extern struct BankAccount a;
    // Declare structure variable a
    // that is defined ELSEWHERE

void print()
{
   printf("a.ID = %d  a.balance = %f\n",
           a.ID, a.balance);
}
Compile error: a undefined

Example how to declare a (global) struct variable

We must declare the global struct BanckAccount variable in struct2b.c:

struct2a.c struct2b.c
/* ---------------------------
   Define the structure FIRST
   ---------------------------- */
struct BankAccount
{
   int    ID;
   double balance;
};

/* ------------------------------------
   THEN you can define a struct variable
   ------------------------------------ */
struct BankAccount a;

int main(int argc, char *argv[])
{
   a.ID  = 123;
   a.balance = 1000.0;

   print(); // Invoke print() defined 
            // inside a different file   
}
/* --------------------------
   Define the structure FIRST
   -------------------------- */
struct BankAccount
{
   int    ID;
   double balance;
};

/* ----------------------------
   THEN you can declare variable
   ---------------------------- */
extern struct BankAccount a;
    // Declare structure variable a
    // that was defined ELSEWHERE

void print()
{
   printf("a.ID = %d  a.balance = %f\n",
           a.ID, a.balance);
}

Example how to declare a (global) struct variable

The struct definition must appear before the struct declaration to provide data type information:

struct2a.c struct2b.c
/* ---------------------------
   Define the structure FIRST
   ---------------------------- */
struct BankAccount
{
   int    ID;
   double balance;
};

/* ------------------------------------
   THEN you can define a struct variable
   ------------------------------------ */
struct BankAccount a;

int main(int argc, char *argv[])
{
   a.ID  = 123;
   a.balance = 1000.0;

   print(); // Invoke print() defined 
            // inside a different file   
}
/* --------------------------
   Define the structure FIRST
   -------------------------- */
struct BankAccount
{
   int    ID;
   double balance;
};

/* ----------------------------
   THEN you can declare variable
   ---------------------------- */
extern struct BankAccount a;
    // Declare structure variable a
    // that was defined ELSEWHERE

void print()
{
   printf("a.ID = %d  a.balance = %f\n",
           a.ID, a.balance);
}

DEMO: demo/C/set2/struct2a.c + struct2b.c

The importance of using header files

To avoid repeating a struct definition (can result in errors !), we should use header files:

struct2a.c struct2b.c
/* ---------------------------
   Define the structure FIRST
   ---------------------------- */
#include "bankaccount.h"

 
 


/* ------------------------------------
   THEN you can define a struct variable
   ------------------------------------ */
struct BankAccount a;

int main(int argc, char *argv[])
{
   a.ID  = 123;
   a.balance = 1000.0;

   print(); // Invoke print() defined 
            // inside a different file   
}
/* --------------------------
   Define the structure FIRST
   -------------------------- */
#include "bankaccount.h"

 
  


/* ----------------------------
   THEN you can declare variable
   ---------------------------- */
extern struct BankAccount a;
    // Declare structure variable a
    // that was defined ELSEWHERE

void print()
{
   printf("a.ID = %d  a.balance = %f\n",
           a.ID, a.balance);
}

NO Demo