Lu4*_*Lu4 2 c visual-studio-2010
我在编译C应用程序时遇到问题,显示的错误毫无意义.我不知道从哪里开始寻找解决方案.
这是代码:
static char* FilterCreate(
void* arg,
const char* const* key_array, const size_t* key_length_array,
int num_keys,
size_t* filter_length) {
*filter_length = 4;
char* result = malloc(4); // error: error C2143: syntax error : missing ';' before 'type' C:\Projects\myleveldb\db\c_test.c
memcpy(result, "fake", 4);
return result;
}
Run Code Online (Sandbox Code Playgroud)
这是全屏截图:
什么可能导致这样的错误?
AnT*_*AnT 29
您正在使用C89/90编译器编译C代码.
在经典C(C89/90)中,在块的中间声明变量是非法的.必须在块的开头声明所有变量.
一旦你开始编写语句,就像*filter_length = 4
,这意味着你完成了声明.您不再被允许在此块中引入变量声明.将您的声明移到更高位置并编译代码.
在C语言中,声明不是语句(与C++相反,声明只是一种语句形式).在C89/90中,复合语句的语法是:
compound-statement:
{ declaration-list[opt] statement-list[opt] }
Run Code Online (Sandbox Code Playgroud)
这意味着所有声明必须首先出现在块的开头.
请注意,在C99声明中也不是语句.但复合语句的语法已更改为:
compound-statement:
{ block-item-list[opt] }
block-item-list:
block-item
block-item-list block-item
block-item:
declaration
statement
Run Code Online (Sandbox Code Playgroud)
这就是为什么你可以在C99中交错声明和语句.