9.2 Struct Basics
C uses struct to group related data into one unit. Unlike a union, each struct member has its own storage, so several members can be valid at the same time.
c
typedef struct {
char owner[32];
double balance;
} Account;
void deposit(Account *account, double amount) {
account->balance += amount;
}Use . to access members of a struct variable and -> to access members through a struct pointer:
c
Account a = {"Ada", 100.0};
a.balance = 150.0; // through the variable
Account *p = &a;
p->balance = 200.0; // through the pointerThinking about data and the functions that operate on it together is an important habit for organizing larger C programs.
Loading interactive lab...
Loading concept check...
Loading practice...