「内部配列」を再割り当てする必要はありません。割り当てたメモリの内容はポインタであり、再割り当てするときは、指す場所の内容ではなく、ポインタinput
のみを再割り当てします。input
input
それがどのように機能するかを示す粗い ASCII イメージ:
最初に、配列に単一のエントリを割り当てると、input
次のようになります。
+----------+ +---------------------------+
input -> | input[0] | -> | What `input[0]` points to |
+----------+ +---------------------------+
2 番目のエントリの場所を確保するために再割り当てした後 (つまりinput = realloc(input, 2 * sizeof(char*));
)
+----------+ +---------------------------+
input -> | input[0] | -> | What `input[0]` points to |
+----------+ +---------------------------+
| input[1] | -> | What `input[1]` points to |
+----------+ +---------------------------+
内容、つまりinput[0]
、再割り当て前と同じです。変更されるのは実際のinput
ポインタだけです。