Update a Record Member
A carton of markers arrives after the donation count was recorded. entry.boxes is an integer member, so += 1 works just as it did for an ordinary integer variable. The update changes only that member; entry.item still points to the same name.
c
#include <stdio.h>
struct Donation {
const char *item;
int boxes;
};
int main(void) {
struct Donation entry = {"Markers", 2};
entry.boxes += 1;
printf("%s: %d boxes\n", entry.item, entry.boxes);
return 0;
}
Before Run, trace boxes: it starts at 2, becomes 3 at the update, and is then printed. Without that update the output would be Markers: 2 boxes.
When you have a pointer to a record instead of the record itself, use ->. For example, after struct Donation *p = &entry;, p->boxes means (*p).boxes. The parentheses select the pointed-to record before its member. Both expressions refer to the same boxes member; neither makes a copy.