|
#include <stdio.h> struct BankAccount { int ID; float balance; }; int main( int argc, char *argv[] ) { struct BankAccount john, mary, *p ; john.balance = 500; p = &john; // Now *p ≡ john (*p).balance = (*p).balance + 2000; mary.balance = 500; p = &mary; // Now *p ≡ mary (*p).balance = (*p).balance + 9000; printf("j: %f m:%f\n\n", john.balance, mary.balance); } |
#include <stdio.h> // Operator ->: p->x ≡ (*p).x struct BankAccount { int ID; float balance; }; int main( int argc, char *argv[] ) { struct BankAccount john, mary, *p ; john.balance = 500; p = &john; // Now *p ≡ john (*p).balance = (*p).balance + 2000; mary.balance = 500; p = &mary; // Now *p ≡ mary (*p).balance = (*p).balance + 9000; printf("j: %f m:%f\n\n", john.balance, mary.balance); } |
#include <stdio.h> // Program re-written using -> struct BankAccount { int ID; float balance; }; int main( int argc, char *argv[] ) { struct BankAccount john, mary, *p ; john.balance = 500; p = &john; // Now *p ≡ john p->balance = p->balance + 2000; mary.balance = 500; p = &mary; // Now *p ≡ mary p->balance = p->balance + 9000; printf("j: %f m:%f\n\n", john.balance, mary.balance); } |