C语言 预期标识符或 '(' 在 'for' 之前

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18862105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:28:04  来源:igfitidea点击:

expected identifier or ‘(’ before ‘for’

c

提问by Calvin Hu

/** @file alloc.c */
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>


#define NEW_BLOCK_SIZE 1024 
#define ARRAY_SIZE 31


typedef struct _metadata_mem
{
size_t size;
void * addr ;
struct _metadata_mem * next_free ;
struct _metadata_mem * pre_free;
char * unit ; 


} metadata_mem;

#define SIZE_OF_MATEDATA sizeof(metadata_mem) 



metadata_mem * array_of_block[31] ; 

int i;
for(i=0; i<31; i++){
    array_of_block[i]=NULL; 
}


int index(size_t size){
int count=0;
while((int)size>=2){
    size/=2;
    count++;
}
return count ; 

}

I received the following error and it starts in the for loop:

我收到以下错误,它从 for 循环开始:

gcc alloc.c -O3 -Wextra -Wall -Werror -Wno-unused-result -Wno-unused-parameter -o    alloc.so -shared -fPIC
alloc.c:30:2: error: expected identifier or ‘(' before ‘for'
alloc.c:30:12: error: expected ‘=', ‘,', ‘;', ‘asm' or ‘__attribute__' before ‘<' token
alloc.c:30:26: error: expected ‘=', ‘,', ‘;', ‘asm' or ‘__attribute__' before ‘++' token
alloc.c:35:5: error: conflicting types for ‘index'
make: *** [alloc.so] Error 1

I have no idea what's wrong with the for loop. It seems OK. Am I not supposed to initialized the array_of_block in the global context ?

我不知道 for 循环有什么问题。看起来还可以。我不应该在全局上下文中初始化 array_of_block 吗?

Thanks a lot.

非常感谢。

回答by David Sainty

The code needs to be inside a function. If this code is the only code in the program, the function is called "main". In its (almost) simplest form:

代码需要在函数内部。如果此代码是程序中的唯一代码,则该函数称为“main”。以其(几乎)最简单的形式:

int main() {
    ... your code
}

回答by Calvin Hu

It's been a while, but that "for" statement needs to be wrapped in a function, doesn't it? Don't think you can do procedural statements at the file block level.

已经有一段时间了,但是“for”语句需要包含在一个函数中,不是吗?不要认为您可以在文件块级别执行过程语句。