Structs
Field Access
The dot operator reads or writes a field inside a struct value.
Field Access
field_access.c
#include <stdio.h>
struct Account {
int balance;
int limit;
};
int main(void) {
int deposit = ;
struct Account account = {20, 30};
account.balance = account.balance + deposit;
printf("balance=%d\n", account.balance);
return 0;
}
#include <stdio.h>
struct Account {
int balance;
int limit;
};
int main(void) {
int deposit = ;
struct Account account = {20, 30};
account.balance = account.balance + deposit;
printf("balance=%d\n", account.balance);
return 0;
}
#include <stdio.h>
struct Account {
int balance;
int limit;
};
int main(void) {
int deposit = ;
struct Account account = {20, 30};
account.balance = account.balance + deposit;
printf("balance=%d\n", account.balance);
return 0;
}
dot operator
`account.balance` selects the `balance` field of `account`.
update field
Assigning to a field changes only that part of the struct.