|
|
Note: things inside [ .... ] are optional elements
|
Comment: the struct variable definitions will allocate memory to store all fields in the struct data type !
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.
|
|
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);
}
|
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 definition