What does while(*pointer) means in C?

后端 未结 5 2081
心在旅途
心在旅途 2020-12-15 12:45

When I recently look at some passage about C pointers, I found something interesting. What it said is, a code like this:

char var[10];
char *pointer = &v         


        
5条回答
  •  旧巷少年郎
    2020-12-15 13:21

    *pointer means exactly what it says: "Give me the value that's stored at the place that the pointer points to". Or "dereference pointer" for short. In your concrete example, dereferencing the pointer produces the one of the characters in a string.

    while(*pointer) also means exactly what is says: "While the expression *pointer yields a true value, execute the body of the loop".

    Since C considers all non-zero values as true, using *pointer in a condition is always equivalent to using the expression *pointer != 0. Consequently, many C programmers omit the != 0 part in order to practice boolean zen.

提交回复
热议问题