Search code examples
cfor-loopwhile-loopcomma-operator

C - while operator in for loop initializer


Why I cannot write smthng like this?

int i, size;
int *arr;
...
for(i = size - 1, while(arr[i] == 0) i--; i >= 0; i--) { ... }

Solution

  • This is just no valid syntax in C.

    A Solution for what you want might be:

    int i, size;
    int *arr;
    ...
    for(i = size - 1; i >= 0; i--) {
        if (arr[i] == 0)
            continue;
        ...
    }