Struct Pointers
Completed
Write void birthday(Person *p) that increases the age using p->age. Read a person and a number of birthdays, apply them, and print name is age.
Input
Line 1: name age. Line 2: birthdays.
Output
One line.
Example
Input
ann 30 3
Output
ann is 33
Editor
C
Hints
p->age is shorthand for (*p).age.
Solution explanation
Passing a pointer to a struct lets the function modify it and avoids copying the whole struct.
Show solution
#include <stdio.h>
typedef struct {
char name[30];
int age;
} Person;
void birthday(Person *p) {
p->age++;
}
int main() {
Person p;
int times;
scanf("%s %d %d", p.name, &p.age, ×);
for (int i = 0; i < times; i++) birthday(&p);
printf("%s is %d\n", p.name, p.age);
return 0;
}