以下は、名前の一覧の中から1つずつ指定して、あいさつを表示するプログラムです。ただし、コメントの部分に入れるべきコードが隠れています。
![](/wp-content/include/images/avatar/teacher/5.png)
どんなコードが入るか分かりますか?
main.c
#include <stdio.h>
#include <stdlib.h>
void GreetTo(int nameIndex, const char **ppNames) {
printf("Hello, %s!\n", ppNames[nameIndex]);
}
int main(void) {
/* ここに入るコードは? */ = {
"Leo",
"Yuki"
};
GreetTo(0, names);
GreetTo(1, names);
return EXIT_SUCCESS;
}
実行結果
Hello, Leo!
Hello, Yuki!
![](/wp-content/include/images/avatar/student/3.png)
おお〜、穴埋め問題ですね!
![](/wp-content/include/images/avatar/teacher/4.png)
たまにはこういうのもいいでしょ?
![](/wp-content/include/images/avatar/student/4.png)
はい。ちょっと楽しそうです!
![](/wp-content/include/images/avatar/student/7.png)
えっと、これがあいさつを表示する関数かな。……ん?
void GreetTo(int nameIndex, const char **ppNames) {
printf("Hello, %s!\n", ppNames[nameIndex]);
}
![](/wp-content/include/images/avatar/student/9.png)
あのー、
**
っていうのは、タイプミスか何かですかね?
![](/wp-content/include/images/avatar/teacher/6.png)
ああ、それはポインタのポインタよ。
![](/wp-content/include/images/avatar/student/11.png)
うわぁ、何ですかそれ?
![](/wp-content/include/images/avatar/teacher/7.png)
うーん、ちょっと難しかったかな。
![](/wp-content/include/images/avatar/student/12.png)
はい、あんまりイメージがわかないです。
![](/wp-content/include/images/avatar/teacher/5.png)
それじゃあ、似ているけどもう少し簡単なプログラムで練習しましょ。
main.c
#include <stdio.h>
#include <stdlib.h>
void PrintNumber(int numberIndex, int *pNumbers) {
printf("Number = %d\n", pNumbers[numberIndex]);
}
int main(void) {
int numbers[] = {
123,
456
};
PrintNumber(0, numbers);
PrintNumber(1, numbers);
return EXIT_SUCCESS;
}
実行結果
Number = 123
Number = 456
![](/wp-content/include/images/avatar/student/3.png)
あ、これなら分かりそうですよ!
![](/wp-content/include/images/avatar/student/7.png)
整数の配列があって……
int numbers[] = {
123,
456
};
![](/wp-content/include/images/avatar/student/8.png)
この関数にインデックスと配列を渡して、要素を1つ表示してる!
void PrintNumber(int numberIndex, int *pNumbers) {
printf("Number = %d\n", pNumbers[numberIndex]);
}
![](/wp-content/include/images/avatar/teacher/4.png)
ここまでは大丈夫そうね。それじゃあ、問題のプログラムに戻ってみてね。
![](/wp-content/include/images/avatar/student/4.png)
はーい!
![](/wp-content/include/images/avatar/teacher/7.png)
「名前の一覧」を表すにはどんな型が必要か考えてみましょう。