C's struct construct allows users to define a "class"-like structure:
#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); } |
After defining the struct data type, we can define struct typed variables
|
Consider the following program with 2 struct BankAccount variables:
#include <stdio.h>
struct BankAccount
{
int ID;
float balance;
};
int main( int argc, char *argv[] )
{
struct BankAccount john, mary; // 2 struct BankAccount variables
john.balance = 500;
mary.balance = 500;
printf("j: %f m:%f\n\n", john.balance, mary.balance);
}
|
We define a pointer variable p to a struct BankAccount typed variable:
#include <stdio.h> struct BankAccount { int ID; float balance; }; int main( int argc, char *argv[] ) { struct BankAccount john, mary, *p ; // p can store a reference to a struct BankAccount var john.balance = 500; mary.balance = 500; printf("j: %f m:%f\n\n", john.balance, mary.balance); } |
The pointer variable p can store an address of a struct BankAccount variable
&john is a reference (= address) of struct BankAccount variable:
#include <stdio.h> struct BankAccount { int ID; float balance; }; int main( int argc, char *argv[] ) { struct BankAccount john, mary, *p ; // p can store a reference to a struct BankAccount var john.balance = 500; &john mary.balance = 500; printf("j: %f m:%f\n\n", john.balance, mary.balance); } |
Therefore: we can assign &john to p:
#include <stdio.h> struct BankAccount { int ID; float balance; }; int main( int argc, char *argv[] ) { struct BankAccount john, mary, *p ; // p can store a reference to a struct BankAccount var john.balance = 500; p = &john; // Now *p ≡ john mary.balance = 500; printf("j: %f m:%f\n\n", john.balance, mary.balance); } |
After the assignment, *p will be an alias for john
And we can use the expression (*p).balance to update john.balance
#include <stdio.h> struct BankAccount { int ID; float balance; }; int main( int argc, char *argv[] ) { struct BankAccount john, mary, *p ; // p can store a reference to a struct BankAccount var john.balance = 500; p = &john; // Now *p ≡ john (*p).balance = (*p).balance + 2000; // Updates john.balance mary.balance = 500; printf("j: %f m:%f\n\n", john.balance, mary.balance); } |
Note: we need to use (*p).balance because of operator precedence... (later)
&mary is a reference (= address) of struct BankAccount variable:
#include <stdio.h> struct BankAccount { int ID; float balance; }; int main( int argc, char *argv[] ) { struct BankAccount john, mary, *p ; // p can store a reference to a struct BankAccount var john.balance = 500; p = &john; // Now *p ≡ john (*p).balance = (*p).balance + 2000; // Updates john.balance mary.balance = 500; &mary printf("j: %f m:%f\n\n", john.balance, mary.balance); } |
So we can assign &mary to p:
#include <stdio.h> struct BankAccount { int ID; float balance; }; int main( int argc, char *argv[] ) { struct BankAccount john, mary, *p ; // p can store a reference to a struct BankAccount var john.balance = 500; p = &john; // Now *p ≡ john (*p).balance = (*p).balance + 2000; // Updates john.balance mary.balance = 500; p = &mary; // Now *p ≡ mary printf("j: %f m:%f\n\n", john.balance, mary.balance); } |
After the assignment, *p will be an alias for mary
And we can use the expression (*p).balance to update mary.balance:
#include <stdio.h> struct BankAccount { int ID; float balance; }; int main( int argc, char *argv[] ) { struct BankAccount john, mary, *p ; // p can store a reference to a struct BankAccount var john.balance = 500; p = &john; // Now *p ≡ john (*p).balance = (*p).balance + 2000; // Updates john.balance mary.balance = 500; p = &mary; // Now *p ≡ mary (*p).balance = (*p).balance + 9000; // Updates mary.balance printf("j: %f m:%f\n\n", john.balance, mary.balance); } |
DEMO: demo/C/set2/ref2struct1.c
|
|
We can access the members of john if we know its location (= address) !
|
|
|
|