1

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.

Summary: when do you just use variable name of pointer without the dereference and when do you use it with dereference. And how to tell difference.

8
  • 4
    The answer is in your code comments .. Commented Sep 30, 2016 at 3:49
  • @HAG, I'm not in school, I'm trying to learn so I didn't understand fully what it meant, but thanks for downvoting a beginner because I didn't understand. I thought the purpose of posting here was if I needed help. It might have been easy for you to understand what those comments meant, but I'm sure when you first started out things were confusing too in some aspects. Commented Sep 30, 2016 at 4:23
  • I didn't downvote ! However, I do think that you can find an answer with simple online search .. there are tons of tutrial that can help you on what you asking for. That's probably why your question is downvoted ... Commented Sep 30, 2016 at 4:31
  • @HAG, well then I apologize. And I did google search. Tried for a while but couldn't find my specific ask. It was just questions about why use pointers and pointers over references. I don't know why I got downvoted for asking a question. Commented Sep 30, 2016 at 4:32
  • stackoverflow.com/questions/897366/… Commented Sep 30, 2016 at 4:33

4 Answers 4

2

A pointer is a variable whose value is the address of another variable or object. We say that the pointer "points to" that other object.

When writing an expression, cp means the pointer variable. *cp means the variable or object whose address the pointer variable holds.

Try to keep clear in your mind the distinction between "the pointer" and "the thing being pointed to". These are two distinct things, each with their own lifetimes and storage requirements.

So to address the code in your question, if (cp) is testing the pointer. It's short for if ( cp != nullptr ), i.e. has the pointer been set to point somewhere? (In other words, does the pointer currently hold the address of another object?)

if (*cp) means if (*cp != 0) , it is asking about the value of the object whose address is stored in the pointer (in other words, the value of the object being pointed to).

Sign up to request clarification or add additional context in comments.

18 Comments

So without dereference is seeing if it is actually pointed to an object and dereference is comparing the actual value of that object?
@Matt That is correct.
@Matt well it depends what you do with the expression afterwards. The thing to understand is that cp refers to the pointer, *cp refers to the pointed-to object. (Both of those things are "actual values"). You could then go on to compare those against something, or assign to them, or do addition, or anything else
@Matt where would you put a * in that line ?
@Matt I guess you mean auto *pbeg = ia.begin(); ? If so, then there is no dereference operator in that line. Operators only occur in expressions, but *pbeg is a declarator. Symbols have different meanings in declarators than in expressions. Try to consult your learning materials for its section on declarations. Also look up how auto deduction works as it is different to other types in a declaration.
|
2

The difference is simple but takes practice to get used to. Essentially, the dereference operator is used when you want to deal with what the pointer is actually pointing to (i.e the thing that is actually at the end of the pointer).

So for example, if I have a char *cp = get_string();, then cp is a pointer that is pointing to the first character in an array of characters.

Checking something like if(cp) is checking whether the pointer is 0 (i.e. if the pointer is pointing to NULL). This is probably useful in this context in that get_string() returns a pointer to a string if it is successful, or a NULL if something went wrong.

In contrast, doing something like while(*cp) is saying, while the character currently pointed to by cp is not 0 (the null character \0), then continue looping. In this context, your while loop probably looks something like:

while(*cp) {
    // process letter currently pointed to by cp
    cp++; // advance cp to point to the next character.
}

This is a common idiom to iterate through a character array.

1 Comment

Why did you put the pointer in parentheses and the postfix increment outside?
1

This image explains the concept. enter image description here

A pointer variable always stores the address of another variable. In case of arrays, it stores the address of the first element in the array.

The dereference operator refers to the value in the address stored by the particular pointer variable. In the above example, the address stored in the pointer variable ptr is 1001. *(ptr) should give the value stored in address location 1001 which is the content of the variable var ie, 50.

In summary, When you want to check if the pointer variable is pointing to some address location, you use the variable without dereference operator and when you want to deal with the value stored in the address location stored in the pointer variable, use it with the dereference operator

In your example, First it checks if the pointer points to a string by the statement

if(cp)

Then it is checking if the content of the variable it the pointer is non null (May be to check the end of the string)by the statement

while(*cp)

1 Comment

Thank you for your help.
0

I don't think writing these two tests like this is good practice. For clarity (and this is the very question you ask) they should be:

if(cp == nullptr) // or `cp == NULL`
    ...
if(*cp == '\0')
    ...

8 Comments

I'm just learning not trying to make a program. I'm trying to lunderstabd the fundamentals.
It is convention to avoid the redundancy of if(cp == NULL). It's like writing if(condition == True); it is considered excessive.
@Matt What I mean is, the first case check for the nullity of the pointer, and the second for the nullity of the pointed character. And if you do *cp with a null pointer you'll cause a segmentation fault, because it is not a valid memory address.
@gowarth I think it depends on context and personal style. For beginners or pieces of code where it may not be obvious what is tested, it is still good practice, IMO.
@gowrath Indeed, it is redundant. But he wrote for clarity. Making things explicit can clarify them.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.