DEV Community

Cover image for A light introduction to C++ Pointers.
Tristan Elliott
Tristan Elliott

Posted on

A light introduction to C++ Pointers.

Introduction

  • This series is going to be dedicated to first understanding the basics of C++ syntax and then evolve into learning data structures and algorithms in C++. For this series I will be reading Data Structures and algorithms in C++ Fourth edition by Adam Drozek. The book is very expensive on Amazon by this is the internet, so I am sure you can find a cheaper PDF version.

Pointers

  • Variables in a program can be considered boxes that are never empty; they are filled with some content either by us the programmer or by the operating system. This kind of variable has at least two attributes:

1) The content/value of the variable
2) The location of the variable in computer memory

  • The content can be a number, character, or a compound item such as a struct or union. However, the content can also be the location of another variable. Variables with such contents are called pointers. Pointers are nothing fancy they quite literally point to other variables.

Some examples


int i = 35, j, *p, *q;

Enter fullscreen mode Exit fullscreen mode
  • In the example above i and j are normal numerical variables, while p and q are pointers. Notice the * this indicates to us and the compiler that these variables are pointers.

  • Since pointers are meant to hold memory addresses we can not do normal assignments to them. for example:

p = 19;// compile error

p = &i; // correct 
Enter fullscreen mode Exit fullscreen mode
  • If you try to do the first assignment you will get an error stating, cannot convert from int to int*. Which basically is telling us that we are assigning the wrong type of variable. The correct way to assign an address to a pointer is shown with p = &i;. The &(ampersand) is called the address-operator and it returns the address of an object in memory. So with the statement p = &i;, we are saying, put the memory address of i into the pointer p.

  • I quickly want to point out the * has different meanings depending on the context which it is used. So please think carefully when using it.

  • Now, it is important for us to be able to distinguish between the value of p(memory address), from the value of the location whose address the pointer holds. i's value is 35.

p = &i;
*p = 20; // what is really going on here?
Enter fullscreen mode Exit fullscreen mode
  • The * in this context is called the indirection operator and it forces the system to first retrieve the contents of p, then access the location whose address has just been retrieved from p and only after that assign 20 to this location. So in the example above we are actually assigning the value of 20 to i.

Why use pointers?

  • I honestly am not sure yet, I have still a lot of pages to read on pointers, so more about pointers to come. This blog post is meant to serve as a simple introduction to pointers and try to show that they are not as scary as they may seem.
  • However, a actual answer to this question can be found HERE on stack overflow.

Conclusion

  • Thank you for taking the time out of your day to read this blog post of mine. If you have any questions or concerns please comment below or reach out to me on Twitter.

Top comments (0)