Ameen
Ameen

Reputation: 1907

How does this condition hold

I have the following function which is part of the Trie structure implementation:

int alpha_char_strlen (const AlphaChar *str) {
    const AlphaChar *p;
    for (p = str; *p; p++) ;
    return p - str;

}

Can any one help me and explain how does the condition for the for loop hold, and precisely what is the condition in this case?
Note: AlphaChar is simply a typedef with unsigned int type and the function counts the AlphaChar characters.

Upvotes: 0

Views: 102

Answers (2)

Kerrek SB
Kerrek SB

Reputation: 477150

The condition *p is identical to *p != 0 (if *p is of a primitive type). That is, you keep incrementing the pointer p until it points to a zero. In other words, you treat str as the pointer to a zero-terminated array.

Upvotes: 8

Mr Lister
Mr Lister

Reputation: 46579

The condition is *p, which is short for *p!=0.

So it simply scans the AlphaChar array for an occurrence of 0.

Upvotes: 0

Related Questions