|
Here a some that you have already learned:
Ordinary way | "Nicer looking" way ----------------+----------------------- i = i + 1 | i++ // The ++ is a new operator !!! i = i - 1 | i-- i = i + 7 | i += 7 .... | .... |
|
Therefore:
|
This occur so often than C has introduced a short-hand notation !!!
|
Illustrated (using the BankAccount struct):
|
#include <stdio.h> /* ------------------------ Structure definition ------------------------ */ struct BankAccount { int accNum; double balance; }; struct BankAccount a; int main(int argc, char *argv[]) { struct BankAccount b; struct BankAccount *p; // Reference variable to type "struct BankAccount" p = &a; // Now: *p is alias for a p->accNum = 123; // This statement is equal to: a.accNum = 123; p->balance = 1000.0; // This statement is equal to: a.balance = 1000.0; p = &b; // Now: *p is alias for b p->accNum = 444; // This statement is equal to: b.accNum = 444; p->balance = 9999.0; // This statement is equal to: b.balance = 9999.0; printf("a.accNum = %d a.balance = %f\n", a.accNum, a.balance); printf("b.accNum = %d b.balance = %f\n", b.accNum, b.balance); } |
How to run the program:
|
|