Pointer in C and C++ | The Most Significant Concept that you Can’t Miss

Most of the programmers struggle a lot when it comes to using pointers. But, this tutorial will help you master the basic concepts and pay attention to details which most people generally tend to skip. Pointer in C and C++ is nothing but a variable that is used to store the memory addresses of other variables.

1. Pointer in C/C++

As we already know, after declaring a variable in the C/C++ programming language, the compiler automatically allocates a reserved amount of memory for the variable depending on its data type.

For example, a 64-bit compiler reserves 8 bytes of memory for the double data type. Now, this variable has a memory location/address associated with it represented by a numeral through which can be accessed. Just like you have a house address that tells your location, a variable also has an address that tells you where the computer is storing it in its memory.

Pointer in C and C++

Whenever we use the scanf() statement, we use the ampersand symbol (&), called the reference. This reference operator indicates that the memory address is being allocated to the variable.

Pointer in C and C++ is nothing but a way to access a variable by storing its memory location. In programming terminology, A pointer is simply a variable that stores the memory location of another variable.

int number = 8;

Memory location = 4572

Now, how difficult is that to comprehend, huh?

After understanding this concept, you have already made a lot of things easier for yourself waiting ahead for you.

Recommended Reading – Data Types in C

2. Significance of Pointer in C and C++

Now, the question arises, what is the need for a pointer when we can keep things simple by avoiding its use?

Well, it is a well-established fact that pointers are one of the strongest features of the C programming language. But, pointers can prove to be quite risky. Using as wild pointers (uninitialized pointers) may cause your program to behave badly, or worse, crash. There are a couple of programming languages like Python and Java which do not involve the use of pointers.

Here are some of the reasons why we use pointers in C and C++:

  1. We can easily access the memory location of any variable directly and manipulate it according to our convenience.
  2. It facilitates the concept of dynamic memory allocation, making it a much faster language as compared to programming languages that do not support the use of pointers like Python and Java.
  3. We prefer pointers in certain situations to improve the efficiency of certain procedures

3. Pointer Declaration in C and C++

In contrast to the ampersand symbol (&) called the reference operator, the asterisk symbol (*) is called the dereference operator used while defining a pointer.

We use the reference operator to access the memory address of a variable.

Enhance Your Fundamental Skills with Different types of Operators in C

Here is a small code in C that illustrates how the reference operator allows us to access the memory location of a variable:

#include<stdio.h>
int main()
{
printf("Welcome to DataFlair tutorials!\n\n");
int number = 8;
printf("The address of the number %d is: %p\n", number, &number);
return 0;
}

Code-

Pointer Declaration in C

Output-

Output of Pointer Declaration in C

Here is a similar code in C++ that illustrates how the reference operator allows us to access the memory location of a variable:

#include<iostream>
using namespace std;
int main()
{
cout<<"Welcome to DataFlair tutorials!"<<endl<<endl;
int number = 8;
cout<<"The address of the number " << number << " is " << &number <<endl;
return 0;
}

Code –

Pointer declaration in C++

Output-

Output of Pointer declartion in C++

Now, let us move on to the actual declaration of the pointer.

Pointers in C and C++ are nothing but variables and therefore as declared in a similar fashion with the addition of a unary operator called the dereference operator (*) before the identifier name of the pointer.

We can do it in 2 ways:

  1. data_type *pointer: Here, the dereference operator is placed just before the identifier.
  2. data_type* pointer: Here, the dereference operator is placed after the data type of the pointer.

Tokens in C – A concept you can’t afford to miss out

For instance,

int *p1; // Here p is a pointer to an integer
char* p2; // Here p is a pointer to a character
float *p3; // Here p is a pointer to a floating-point variable

Key takeaway:

  1. Every time you compile the program, you would get a different memory location as the compiler allocates memory after compilation.a
  2. The numerical value of the memory address has no mathematical relevance as to why the compiler chooses it. It is simply a name given to the address of the variable.

4. Initialization of Pointer in C and C++

It is important to note that it is a dangerous practice to leave the pointer uninitialized as it may cause the program to crash.

This is how we can initialize pointers in C:

int number = 5;
int *p;
/* The address of the variable number is stored in p that points to the location of number */
p = &number;

It is important to note that the referencing operator and the dereferencing operator are opposite to each other.

Here is a diagrammatic representation of how the memory is allocated and how it is accessed using a pointer:

Pointers Example

5. Dynamic Memory Allocation (DMA) using Pointers

As discussed above, C offers the implementation of dynamic memory allocation with the help of pointers.

In the C programming language, memory can either be allocated statically or dynamically.

Just like static memory allocation is done with the help of arrays when the amount of memory to be allocated is known beforehand, dynamic memory allocation is done with pointers when the amount of memory to be allocated is unknown.

Get the Samurai Technique to Learn Arrays in C

There are basically two keywords helpful while working with DMA:

  1. new: We use it to allocate memory dynamically and to return a pointer that stores the memory address of the variable or data structure.
  2. delete: We use it for the deallocation of memory that is pointed by the pointer.

6. How do Pointers Work in C and C++?

After developing an understanding of what pointers are and how we can define them, now let us try to understand how they work.

Pointer in C

Here is a code in C that illustrates how pointers work:

#include <stdio.h>
int main()
{

printf("Welcome to DataFlair tutorials!\n\n");

int* pvariable;
int variable = 10;

printf("The value of the variable is: %d\n", variable);
printf("The address of the variable is: %p\n", &variable);

pvariable = &variable; // After assigning the address of variable to the pointer
printf("The value stored in the pointer is: %d\n", *pvariable);
printf("The address of the pointer is: %p\n", pvariable);

variable = 20; // Changing the value of the variable
printf("The value stored in the pointer is: %d\n", *pvariable);
printf("The address of pointer pc: %p\n", pvariable);

*pvariable = 2; // Changing the value of the pointer
printf("The value of the variable is: %d\n\n", variable);
printf("The address of the variable is: %p\n", &variable);

return 0;

}

Code-

How Pointer works in C

Output-

Output of Working of Pointer in C

Pointer in C++

Here is a code in C++ that illustrates how pointers work:

#include <iostream>
using namespace std;

int main()
{
cout<<"Welcome to DataFlair tutorials!"<<endl<<endl;

int* pvariable;
int variable = 10;

cout<<"The value of the variable is: "<< variable <<endl;
cout<<"The address of the variable is: "<< &variable <<endl<<endl;

pvariable = &variable; // After assigning the address of variable to the pointer
cout<<"The value stored in the pointer is: "<< *pvariable <<endl;
cout<<"The address of the pointer is: "<< pvariable <<endl<<endl;

variable = 20; // Changing the value of the variable
cout<<"The value stored in the pointer is: "<< *pvariable <<endl;
cout<<"The address of pointer pc: "<< pvariable <<endl<<endl;

*pvariable = 2; // Changing the value of the pointer
cout<<"The value of the variable is: "<< variable <<endl;
cout<<"The address of the variable is: "<< &variable <<endl;

return 0;

}

Code –

how Pointers works in C++

Output-

Output of working of Pointers in C++

Elucidation

  1. The memory address of a variable can be accessed with the help of the reference operator ‘&’.
  2. After assigning the memory address of the variable to the pointer, the value of the variable can be accessed by the pointer using the dereference operator.

Key takeaway: It is important to note that the ‘%u’ operator doesn’t support the Linux operating system, hence we get the memory addresses of variables in hexadecimal form with the help of ‘%p’ format specifier.

Simplify your Long Codes using Recursive Functions in C and C++

7. Common Mistakes while working with Pointer in C and C++

Here are some of the common mistakes programmers generally tend to make while working with pointers:

1. The incorrect data type for pointers

It is important to note that in order to avoid the loss of data, it is very important to assign the correct data type to the pointer.

For instance, if the data type of the pointer is of integer type, the C compiler considers the variable whose address the pointer would point to will store integer values and hence the compiler reserves 4 bytes of space for it when talking about a 64-bit compiler.

Let us consider this fragment of code:

/* Here both the variables number1 and number2 are of long data type which occupies 8 bytes */

long number1, number2; int *p;

scanf(“%ld”, &number1); // To input value of number1 as long data type
p = &number1; // To store the address of number1 into the pointer p
number2 = *p; // To store the value being pointed to by p into number2

2. Incorrect initialization

We tend to confuse the value of the variable and its memory address.
Let’s say we have one variable ‘a’ of int type and a pointer *p again of int type,
Then the following initializations are incorrect:

a = p;
*p = &a

It is important to understand that it is incorrect as ‘a’ is a variable whereas p is its memory address.

8. Summary

In this tutorial, we discussed the basic meaning of pointers and their use. We saw how we can declare and initialize pointers in C and C++. We even understood how dangerous it is to leave a pointer uninitialized.

Thereafter, we saw how we can implement the concept of dynamic memory allocation with the help of pointers. Then, we saw how a pointer works in C and C++ with the help example. Finally, we concluded our discussion by pointing out the common mistakes made by programmers while working with pointers.

The Next Step – Master the Concepts of Stacks and Queues (LIFO & FIFO) in C

We would be happy to serve you in case you find any difficulties within this article. Leave your comments below.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.