Does C have a “foreach” loop construct?

前端 未结 12 2202
情书的邮戳
情书的邮戳 2020-12-02 03:49

Almost all languages have a foreach loop or something similar. Does C have one? Can you post some example code?

12条回答
  •  -上瘾入骨i
    2020-12-02 04:19

    There is no foreach in C.

    You can use a for loop to loop through the data but the length needs to be know or the data needs to be terminated by a know value (eg. null).

    char* nullTerm;
    nullTerm = "Loop through my characters";
    
    for(;nullTerm != NULL;nullTerm++)
    {
        //nullTerm will now point to the next character.
    }
    

提交回复
热议问题