pointers in c++

I am studying pointers in C + + and in my book there is a paragraph that explains what values ​​can take a pointer

1) it can point to an object
2) it can just point to the location immediately past the end of an object
3) it can be a null pointer
4) it can be invalid

can you explain me point 2 ? what does it mean ? can you make me an example?

my book say :

two pointers are null if :

1) they are both null
2) they address the same object
3) or if they are both pointers one past the same object??

can you explain me point 3 ?

when will i need to use pointers again ? for now i'm just using them to change the value of an object......

Last edited on
Pointers have to be able to point the the location right past an object for a few reasons. One classic example is an array of 0 elements. A pointer of such array would always point to one past the last element, because there are no elements.

In C++, it's used a lot for denoting range of objects. Having a pointer point to one space past the object allows code to check against that pointer to see if it is still within the object or if it has reached the end.

I do not understand those last three questions. Maybe if you provide an exact quote instead of your own paraphrasing it might be more clear.

When will I need to use pointers again?

When you want to pass around the address of an object instead of the object itself. Or when you need to create something on the heap. In modern code, you should rarely be using raw pointers, but instead be using smart pointers. As you get more experience, you will understand their importance.
For the last question: "Two pointers are NULL if:", the answer is 1. They are both NULL.

A pointer will only be NULL if it has been assigned the value of NULL.

2 is wrong because they can't be NULL since the point to some object.

3. is wrong because the pointer points to some location, it may not be a valid location but it's a location.

Remember a NULL pointer points "no where".

For your first question about point 2, a pointer can point anywhere, even to a point just past the end of an object.
Study linked list and you'll understand everything
Topic archived. No new replies allowed.