Expected identifier or '(before' for

/** @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 got the following error and it starts in a for loop:

 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 don't know what happened to the for loop. Everything seems to be in order. Shouldn't I initialize array_of_block in a global context?

Many thanks.

+4
source share
2 answers

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

 int main() { ... your code } 
+7
source

It has been a while, but this “for” statement needs to be wrapped in a function, right? Do not think that you can do procedural instructions at the file block level.

+3
source

Source: https://habr.com/ru/post/1502706/


All Articles