A Beginners Guide to Pointers in Programming.

A Beginners Guide to Pointers in Programming.

Pointers looks like a very daunting thing, especially to a beginner. Pointers are always tricky to understand but if you have your fundamentals clear, then you won't fear pointers. So let's get started.

Introduction.

Pointers are used to store/point to the address of a variable or a memory location. Pointers is an important core concept of C & C++. Not only language specific, but pointers play a key part in the internal implementation of Linked Lists, Trees, and Tries. So how does a pointer look like, how to create one, and what are its uses?

Syntax

Before understanding the syntax of pointers, let's see how we can get an address of a variable first.

int x = 10;
cout<< &x<<endl;

Well, that's it. The & operator is used to know the address of any variable. Now to store the address, we need a pointer. This is how a pointer is created.

int x = 10;
int *ptr = &x;   // data_type *(pointer_name);

cout<< "Value of x"<< *ptr<< endl;  // access the value of the variable
cout<<"Address of x"<< ptr<<endl; // access the address of the variable

// We can directly change the value of the variable through pointers. 

*ptr = 250;
cout<<"Value"<< *ptr<<endl;

See, that's how easy it is to create and use pointers. The tricky part is the internal working of it. Have a look at this picture to see what is actually happening behind the scenes.

pointer-memory-representation.png

As you can see the above image, the ptr variable is storing an address of the x variable. So whenever we cout ptr, we get an address as an output. But when we use the *ptr notation, we get the contents of variable x.

Pointers in Arrays

We can perform arithmetic operations on pointers as well. But, they are only useful when we are dealing with an array.

This is how we can use a pointer to point to the starting address of an array.

int arr[3] = {1,2,3}
int *ptr;
ptr = arr;  // assigns the address of arr[0] to ptr

for(int i=0;i<3;i++)
{
      cout<< *ptr<<" "<<endl;
      ptr++;   // takes the pointer to next address.
}

We can also use:

  • (-) to decrement the pointer location
  • (+) to add new value to current value of the pointer and - to subtract an integer from a pointer.
  • Adding two addresses makes no sense, because there is no idea what it would point to.
  • Subtracting two addresses lets you compute the offset between these two addresses.

Applications of Pointers

  • To pass arguements by reference. (It helps in modifying the variable in some other function)
  • Dynamic Memory Allocation
  • To implement Linked Lists, Trees, etc.
  • Useful in system level programming where many addresses are used.

Conclusion.

So this sums up our article on Pointers. We have just discussed the very basic concepts of pointers. We have not even talked about pointers with multi-dimensional arrays and other advanced concepts.

The aim of this article was to make you comfortable with basic pointers as a beginner. If you want to get the complete knowledge of pointers, go and watch this video. This is probably the best video out there on the internet that explains pointers so well.

In the next article, we will have a look at one of the data structures that is created with the help of pointers. Linked Lists.

Did you find this article valuable?

Support CompSciWithIyush by becoming a sponsor. Any amount is appreciated!