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