Converting this code line to C

2019-09-20 12:40发布

I have the following code line:

for ( int i = index; i < al->size; ++i )

//i,index and size are integers.al is an arraylist

When I compile this in C, I get the error:

 'for' loop initial declarations are only allowed in C99 mode

Im not sure on how to fix this.

Thank you!

4条回答
不美不萌又怎样
2楼-- · 2019-09-20 13:01
for ( int i = index; i < al->size; ++i ) 

needs to become

int i;

for (i = index; i < al->size; ++i)
查看更多
We Are One
3楼-- · 2019-09-20 13:10

Just declare int i before the loop.

查看更多
兄弟一词,经得起流年.
4楼-- · 2019-09-20 13:23

Try to declare the i variable first.

int i;
for ( i = index; i < al->size; ++i )
查看更多
贪生不怕死
5楼-- · 2019-09-20 13:27

Either declare the iterator outside of the loop:

int i;

for (i = index; i < al->size; ++i) {
    do_foo();
}

or if your compiler supports it, compile against the c99 or compatible standard:

gcc -std=c99 your_code.c 

(Note that gnu89/gnu90 is the default (as of 4.8, anyway.))

查看更多
登录 后发表回答