簡単なサンプルプログラムはこちら
#include <stdio.h>
#include <string.h>
const char *hello_string = "Hello";
int main(void)
{
char *world_string = " World";
static char hello_world[strlen(hello_string)+strlen(world_string)];
strcpy(&hello_world[0], hello_string);
strcat(&hello_world[0], world_string);
printf("%s\n", hello_world);
return 0;
}
コンパイラ出力:
test.c: In function ‘main’:
test.c:9:13: error: storage size of ‘hello_world’ isn’t constant
static char hello_world[strlen(hello_string)+strlen(world_string)];
^
この場合、「静的」を完全に無用で不必要に使用するとエラーが発生し、それを削除すると問題なくコンパイルされることを認識しています。これは、私の質問を説明するための簡単な例です。
私が理解していないのは、「hello_string」がconst char * として宣言され、そのサイズが実行中に変更されない場合に、ストレージ サイズが一定ではない理由です。これは、コンパイラがそれを知るほど賢くないという単なるケースですか?