c编译器错误"error: variably modified 'string' at file scope"



考虑:

#include <stdio.h>
#include <string.h>
const int STRING_SIZE = 40;
typedef char string[STRING_SIZE];
char typeInput(string message);
int main()
{
    typeInput("Hi my name is Sean");
}
char typeInput(string message)
{
    printf(" %s", message);
}

错误:

错误:在文件作用域处可变地修改了"字符串">

出于某种原因,我一直犯这个错误。我哪里错了?

以防万一,我正在使用Code::Blocks。

在C中,const不声明常量,而是声明只读变量。编译器会抱怨,因为STRING_SIZE不是常量。

解决方法:

enum { STRING_SIZE = 40 };
// enum creates constants in C (but only integer ones)

最新更新