jeudi 29 septembre 2016

When to use dereference operator for pointers C++

I'm currently studying C++. Not in school. Using books, tutorials and practice.

One thing that confuses me and I haven't been able to track down an answer is when to use the dereference operator (*) for pointers. For example, from C++ primer which I'm currently reading:

char *cp = get_string();
if (cp) /* ... */    // true if the pointer cp is not zero 

while (*cp) /*    ... */    // true if *cp is not the null character

I don't understand why in the if statement it is just cp without dereference operator and then in the while statement it uses the dereference operator. There is other examples like a mix of uses in a for loop but this is the latest example in the book that confuses me. Thanks in advance for your help.

Aucun commentaire:

Enregistrer un commentaire