Tag Archives: Blue Bridge Cup

[Solved] Linux Compile Error: error: ‘for’ loop initial declarations are only allowed in C99 mode

1. Problem description

During the development of embedded Linux, compile and report the following errors

src/util/Vector.c:94:9: error: ‘for’ loop initial declarations are only allowed in C99 mode
         for (int j = V->length++; j > i; j--)
         ^
src/util/Vector.c:94:9: note: use option -std=c99 or -std=gnu99 to compile your code
src/util/Vector.c: In function ‘Vector_remove’:
src/util/Vector.c:123:9: error: ‘for’ loop initial declarations are only allowed in C99 mode
         for (int j = i; j < V->length; j++)
         ^

2. Problem analysis

GCC compilation is based on compilation lower than C99 standard. Defining variables within a for loop is not allowed in standards lower than C99.

3. Solution

Method 1

Modify code

int j;
for (j = i; j < V->length; j++)

Method 2

GCC specifies compiling using the C99 standard

gcc -std=c99

Method 3

Specify the C99 standard for compilation in makefile

CFLAGS += -std=c99

[Solved] Stream Error: stream has already been operated upon or closed

public class StreamTest {
    public static void main(String[] args) {
        //Generate a Stream of type String
        Stream<String> stringStream = Stream.of("1", "2", "3", "4", "5");
        // Convert string type in stream to int type using map method
        stringStream.map(
                (String s) ->{
                    return Integer.parseInt(s);
                });
        stringStream.forEach(i -> System.out.println(i));
    }
}

As shown in the figure, the code runs with an error:

The stream has been used or closed

This is a feature of streams: a stream can only be used once!