Pointers in C++

Pointers are a difficult concept to master in C++. With this article, I intend to share with you some basics of pointers in C++. You can find an introduction to address-of operator, void pointer, null pointer and pointer to pointer. You’ll also learn how to create pointers and use them to access underlying objects.

Address-of Operator (&)

The address-of operator returns the memory address of the object with which it is used. The memory address is a hexadecimal number representing a particular location where the variable is stored.

Q. What are Pointers?

Pointers are a specific type of object that point to the memory location of another object. Pointers can point to an object or just past an object. Also, there are null pointers, that don’t point to anything.

To create a pointer, you need to place an * (ass-at-risk) before the variable name.

Here, we have declared a pointer p that points to an int type. Then, we store the memory address of xxx into p. It is important to mention the data type for the object, the pointer will store. Here the data type is int. By specifying the data type, we can use the pointer to perform actions on the underlying object.

We can also define the variable and and pointer in the same line.

Declarations like the one above can be confusing for beginners. This is why most C++ programmers put the * alongside the variable instead of with the type declaration.

As an object, a pointer has the following properties:

  • Can only store memory address or null
  • Can be reassigned
  • Can itself be a constant object
  • Does not need initialization
  • Can have pointers and references

Here, the first property is very important since it tells us the kind of value a pointer can store. Storing any other kind of value would lead to an error.

Obviously, your compiler may show a different error message. Also, type in the code into your compiler and note down the error message for future reference. This is a good way of learning how to debug your own code.

Then there are invalid pointers. An example of such a pointer is

Accessing an invalid pointer will cause an error. It will be like nuking your own program.

Better to Initialize all Pointers as errors thrown due to uninitialized pointers are difficult to troubleshoot

Q. How to use a Pointer?

Once a memory address has been stored, you can access the value at that memory location. You can even make changes to the underlying object via the pointer. Let’s look at an example:

Here, we first create an int type and a pointer to that int type. The first cout will print a hexadecimal memory address of the integer i.

The second cout prints the integer value stored in i, i.e., 69. * in *p here is a dereference operator and returns the object of which the memory address it stores. In C++ an operator can have different meanings depending on the context.

Pointers are Objects

Since pointers are objects, assignment changes the value of the pointer. Still, you can only change the value to memory address of another object or null.

Just like any other object, it can have a reference and a pointer to a pointer.

Once we have created a pointer to a pointer:

  • Dereferencing once will return the pointer it points to
  • Dereferencing twice will get us the underlying object

Null and Void Pointers

A null pointers doesn’t point to any object.

Both p and p1 here are null pointers. Unlike uninitialized pointers, we can check the value of a null pointer. This is quite helpful in conditional statements where we may want to check if a pointer is null or not.

Void pointers can store memory address of any variable type.

Still, with power comes limitation. (Na Na Na Na Na Na Na Na Spiderman!)

Essentially, void pointers can be used for comparing two different pointers. Trying to operate on the underlying object with the void pointer will only result in an error. In C++, the compiler needs to know the type of a variable in order to perform operations on it. Since, void pointers don’t specify type, they can’t be used for performing operations. Void pointers are very useful when dealing with memory but the information about the same is out of the scope for this article (Topic Buffer Overflow Error…:p).

Conclusion

This article is meant to give you a brief introduction to pointers. There are many other aspects to pointers when using them with maps, arrays, vectors and strings. Hopefully, this article helps you in building a basic knowledge about this object type.

Hope this helped! 😎🤘


Weird Programmer

I hate people who use LOL in face to face conversations. Also, if I tell you anything else about myself, then I'll have to KILL you~~!

Leave a Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of
%d bloggers like this: