Converting this code line to C

偶尔善良 提交于 2019-12-12 06:54:10

问题


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!


回答1:


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.))




回答2:


Just declare int i before the loop.




回答3:


Try to declare the i variable first.

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



回答4:


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

needs to become

int i;

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


来源:https://stackoverflow.com/questions/33457768/converting-this-code-line-to-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!