C - 構造体、共用体、ビットフィールド - 独自の構造を使う - ポインター
Head First C ―頭とからだで覚えるCの基本、 David Griffiths(著)、 Dawn Griffiths(著)、 中田 秀基(監修)、 木下 哲也(翻訳)、 O’Reilly Media)の5章(構造体、共用体、ビットフィールド - 独自の構造を使う - 分割して構築する)、p.239(自分で考えてみよう)の解答を求めてみる。
Makefile
all: main.c
cc main.c && ./a.out
コード
main.c
#include <stdio.h>
typedef struct
{
const char *name;
int age;
} turtule;
void happy_birthday(turtule *t)
{
(*t).age++;
printf("誕生日おめでとう、%s!これで%i才ですね!\n",
(*t).name, (*t).age);
}
int main()
{
turtule t = {"マートル", 99};
printf("%i\n", t.age);
happy_birthday(&t);
printf("%i\n", t.age);
}
入出力結果(Terminal, Zsh)
% make
cc main.c && ./a.out
99
誕生日おめでとう、マートル!これで100才ですね!
100
%