DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Rust’s Ownership and Borrowing Enforce Memory Safety
  • Exploring Exciting New Features in Java 17 With Examples
  • Linked List in Data Structures and Algorithms
  • What Are Protocol Buffers?

Trending

  • Unlocking AI Coding Assistants Part 4: Generate Spring Boot Application
  • Unlocking AI Coding Assistants Part 3: Generating Diagrams, Open API Specs, And Test Data
  • Integrating Security as Code: A Necessity for DevSecOps
  • Unlocking AI Coding Assistants Part 2: Generating Code
  1. DZone
  2. Data Engineering
  3. Data
  4. Getting Deep Into C++ Pointers

Getting Deep Into C++ Pointers

In this article, we'll discuss the use of Pointers in C++. Pointers excel at making code shorter, more compact, and efficient, proving themselves valuable.

By 
Prashanth P user avatar
Prashanth P
·
Apr. 06, 21 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
5.6K Views

Join the DZone community and get the full member experience.

Join For Free

Introduction

C++ being built on top of C language has readily inherited many core features, which include Pointers. Pointers were proven to be handy when it comes to working with data at memory level Statically or Dynamically. Many Programming Languages use Pointers in some or the other way.

Why Bother Learning Pointers?

Yes, it is so true that the same task could be accomplished without the use of pointers, but we must acknowledge that the proper use of pointers can make code more short, compact, and efficient at the same time.

C++ Pointers in Brief

Pointers were mainly introduced to peek and poke the data from memory effectively. Pointers were made to hold the memory addresses (i.e physical address/memory location) so that one could refer to the data at that memory location through pointers.

So What Is a Pointer?

Programmatically, a Pointer is a variable that can hold the address of other variables as its value and can indirectly refer to the contents of that memory address.

Pointer as a Variable

Yes, a pointer is a variable, which means that at some given time it can point to a variable (i.e hold the address of that variable) and next time it can point to some other variable of the same type (datatype).

Pointer as a constant

We can make a pointer constant which means it cannot point to other memory addresses with the use of const qualifier which we will see in the later parts. Before Moving into the Pointers Jargons, consider the following declaration:

int val = 100;

The above statement commands the compiler to

  • Reserve memory space to hold an integer value.
  • Associate name value to it.
  • Assign value 100 to it.

Memory Map for the above statement are as follow:

value of the pointer flow chartAs we can see, the compiler has chosen the memory location 65545 (Machine specific) to store the value 100. We can print this memory address as:

cout << &val; //prints the memory address of variable called val

We can store this address into a variable and refer to the value indirectly through that variable and here comes pointers into the limelight.

Pointers Syntax

Had the pointers been the same as ordinary variables, there wouldn’t have been any need to grasp them separately, so for sure pointers are not ordinary variables. Let’s take a look into the syntax of a pointer.

data_type* variable_name;

Here data_type refers to the data type of the variable whose address is going to be stored in the pointer variable. Alternatively, we can say that pointer is gonna point to a variable whose value is of type data_type.

Some valid examples of pointer declarations are as follows: 

C++
 




x


 
1
int *i_ptr;
2
float * f_ptr;
3
char* c_ptr;
4
string*s_ptr;



Note that the space isn’t necessary but the *(asterisk) should be in between data_type and variable_name.

Pointers Jargon

Consider the following pointer assignment statement:

C++
 




xxxxxxxxxx
1


 
1
int val = 100;
2
int* ptr = &val;



Now we have successfully stored the address of val into ptr thus we can refer to val through ptr as:

C++
 




xxxxxxxxxx
1


 
1
cout <<"Address of val "<< &val <<' '<< ptr;
2
cout <<"Value of val "<< val <<' '<< *ptr;


Two Key Operators

Plain Text
 




x
13


 
1
& (ampersand) - Read as 'Address of' operator
2

          
3
                      Eg. &val ~ Address of val.
4

          
5
* (asterisk) - Read as 'Value at address' operator or dereference operator
6

          
7
                Eg. *ptr ~ Value at the address contained in ptr.
8

          
9
Remember  * in *ptr is different from int* ptr.
10

          
11
int* ptr statement tells the compiler to declare an integer pointer variable.
12

          
13
While *ptr statement tells the compiler to dereference the pointer that gives the content of the memory address stored in ptr.



Working of Pointers

Now since we have the address of val variable in ptr, we can now access and manipulate the data stored at the memory address contained in ptr.

Below is the implementation of a simple program using pointer:

C++
 




xxxxxxxxxx
1
10


 
1
#include<iostream>
2
using namespace std;
3
int main(){
4
    int val = 100;
5
    int* ptr = &val;
6
    cout << val << endl;
7
    *ptr = val / 10;
8
    cout << val << endl;
9
return 0;
10
}



Thus not only we can access val with *ptr but also can change the data (or reassign) to any value of the same data type.

ptr pointer flow chart

Pointers Extended

Since a pointer is a variable, it does occupy a space in memory and we can even store this address into another pointer variable, which would be called pointer to pointer. This way we can create a pointer to pointer to pointer to pointer and so on. For now, we would stick to a pointer to a pointer.

The declarative syntax of pointer to a pointer is given below:

C++
 




xxxxxxxxxx
1


 
1
int  val = 100;
2
int *ptr = &val; //pointer to integer
3
int **d_ptr = &ptr;//pointer to pointer which points to an integer


We can refer to the ptr contents as 

cout << *d_ptr << ptr << &val; //eg 65545 65545 65545 i.e address of val

Since *d_ptr value is an address (address of val) thus we can access the actual data of val through the following statement.

C++
 




xxxxxxxxxx
1


 
1
cout << "value of val"<< val << *ptr << **d_ptr;
2
//prints the value of val i.e 100


To manipulate the content of val through d_ptr we can write as **d_ptr = 2000;

d_ptr = 2000 pointer

Function Returning More Than One Value

We are aware that normally a function can return at most one value at a time. What if we need to return more than one value from a function? The Naive approach would be to make that many functions return different values. The other handy option is to use pointers making code more efficient

Without Pointers

C++
 




xxxxxxxxxx
1
32


 
1
#include<iostream>
2
using namespace std;
3
int areaOfRectangle(int l, int b){
4
    return l*b;
5
}
9
}
6
int perimeterOfRectangle(int l, int b){
7
    return 2*(l+b);
8
}
9
int main(){
10
    int l = 20, b = 30;
11
    int area = areaOfRectangle(l, b);
12
    int peri = perimeterOFRectangle(l, b);
13
    cout << area << endl;
14
    cout << peri << endl;
15
return 0;
16
}


With Pointers

C++
 




xxxxxxxxxx
1
30


 
1
#include<iostream>
2
using namespace std;
3
Void arePeri(int l, int b, int* area, int* peri){
4
    *area = l*b;
5
    *peri = 2 * (l + b);
6
}
7
int main(){
8
    int l = 20, b = 30;
9
    int area;
10
    int peri;
11
    areaPeri(l, b, &area, &peri);
12
    cout<<area<<endl;
13
    cout<<peri<<endl;
14
return 0;
15
}


Types of Pointers

The types of pointers which should be known to a programmer are:

Null pointer

When a pointer doesn’t point to any valid memory location we should make to point it to null (i.e absence of value). int *ptr = NULL;

Constant pointer

When a pointer needs to point to only one memory location throughout its lifetime i.e it should be changed, then we can declare the pointer as constant.

C++
 




xxxxxxxxxx
1


 
1
int val = 100;
2
int *const ptr = &val;//constant pointer


Void pointer

When we are not sure what the pointer is gonna point to .i.e would it point to an int or float or char? In such a situation, we should declare the pointer as a void pointer and explicitly make it point to a variable of any data type as desirable.

C++
 




xxxxxxxxxx
1
13
9


 
1
int age = 20;
2
float height = 6.1;
3
void* ptr = NULL; // making it point to NULL
4
ptr = (int *)&age; // making it point to an integer variable
5
cout << *ptr <<endl; // prints the value of age
6
ptr = (float*)&height: //making it point to a float variable
7
cout << *ptr <<endl; //prints the value of height
12

          


Dangling pointer

When a pointer is trying to point to a memory location that is not available or inaccessible or freed (using a free function). Then the pointer is called a dangling pointer.

C++
 




xxxxxxxxxx
1
10
9


 
1
int  val = 100;
2
int *ptr = &val;
3
cout << *ptr; //prints 100
4
free(ptr);
5
cout << *ptr; //error


Pointers Arithmetic

An awesome feature of pointers is that they can move in memory to and fro. They jump forward or backward with each step equal to the memory size they are pointing to. For eg.

C++
 




xxxxxxxxxx
1
23


 
1
int val = 10;
2
int *ptr = &val;
3
cout << ptr <<endl; //eg 65534
4

          
5
ptr++;
6
//prints 65538 (as typical int is of 4 bytes thus it jumps 4 bytes //i.e 65534 + 1 * size of integer type = 65534 + 4).
7
cout << ptr <<endl;
8

          
9
ptr = ptr - 2;
10
//prints 65530 
11
//(65538 - 2 * size of integer type = 65538 - 2*4 = 65538 - 8).
12
cout << ptr<<endl;


Advantages of Pointers

The major advantages of pointers are:

  • Makes the function return more than one variable at the same time.
  • Makes code way shorter, compact resulting in increased speed of execution.
  • Dynamic memory allocation and deallocation are possible only with the use of pointers.
  • To implement various data structures (eg. Linked Lists, Trees, et)

Conclusion

Thus we can sum up by saying that a pointer is a construct that can point to a memory location and indirectly access and modify the contents inside it. From making function return multiple values to dynamic memory allocation pointers have shown their superpowers. Thus if a solution could be approached with the use of pointers, it should be solved with the use of pointers making code more efficient.

Pointer (computer programming) Memory (storage engine) Data Types Data (computing)

Opinions expressed by DZone contributors are their own.

Related

  • Rust’s Ownership and Borrowing Enforce Memory Safety
  • Exploring Exciting New Features in Java 17 With Examples
  • Linked List in Data Structures and Algorithms
  • What Are Protocol Buffers?

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!