vendredi 8 mai 2015

Why can't I use the whole char [] in while?

In Python, I can do:

str = "abcd"
while(str):
    print str

Although it's not very useful (iterating over the elements in the string is usually what's needed), the fact that it's possible makes me wonder why the equivalent in C is not, So:

char str[] = "abcd"
while(str[])
{
    do stuff
} 

When I try to compile:

isblank.c:11:13: error: expected expression
        while (str[])
                   ^
1 error generated.

And I am forced to add the index of the string in the expression, therefore forcing it to iterate over the elements of the string.

Why so?

Aucun commentaire:

Enregistrer un commentaire