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