Skip to main content

C++ Pointers

In this article, you'll learn everything about pointers. You'll learn how values are stored in the computer and how to access them using pointers.



Pointers are powerful features of C++ that differentiates it from other programming languages like Java and Python.
Pointers are used in C++ program to access the memory and manipulate the address.

Address in C++

To understand pointers, you should first know how data is stored on the computer.
Each variable you create in your program is assigned a location in the computer's memory. The value the variable stores is actually stored in the location assigned.
To know where the data is stored, C++ has an & operator. The & (reference) operator gives you the address occupied by a variable.
If var is a variable then, &var gives the address of that variable.

Example 1: Address in C++

#include <iostream>
using namespace std;

int main()
{
int var1 = 3;
int var2 = 24;
int var3 = 17;
cout
<< &var1 << endl;
cout
<< &var2 << endl;
cout
<< &var3 << endl;
}
Output
0x7fff5fbff8ac
0x7fff5fbff8a8
0x7fff5fbff8a4
Note: You may not get the same result on your system.
The 0x in the beginning represents the address is in hexadecimal form.
Notice that first address differs from second by 4-bytes and second address differs from third by 4-bytes.
This is because the size of integer (variable of type int) is 4 bytes in 64-bit system.

Pointers Variables

C++ gives you the power to manipulate the data in the computer's memory directly. You can assign and de-assign any space in the memory as you wish. This is done using Pointer variables.
Pointers variables are variables that points to a specific address in the memory pointed by another variable.





How to declare a pointer?


int *p;
OR,
int* p;

Reference operator (&) and Deference operator (*)

Reference operator (&) as discussed above gives the address of a variable.
To get the value stored in the memory address, we use the dereference operator (*).
For example: If a number variable is stored in the memory address 0x123, and it contains a value 5.
The reference (&) operator gives the value 0x123, while the dereference (*) operator gives the value 5.
Note: The (*) sign used in the declaration of C++ pointer is not the dereference pointer. It is just a similar notation that creates a pointer.

Example 2: C++ Pointers

C++ Program to demonstrate the working of pointer.
#include <iostream>
using namespace std;
int main() {
int *pc, c;

c
= 5;
cout
<< "Address of c (&c): " << &c << endl;
cout
<< "Value of c (c): " << c << endl << endl;

pc
= &c; // Pointer pc holds the memory address of variable c
cout
<< "Address that pointer pc holds (pc): "<< pc << endl;
cout
<< "Content of the address pointer pc holds (*pc): " << *pc << endl << endl;

c
= 11; // The content inside memory address &c is changed from 5 to 11.
cout
<< "Address pointer pc holds (pc): " << pc << endl;
cout
<< "Content of the address pointer pc holds (*pc): " << *pc << endl << endl;

*pc = 2;
cout
<< "Address of c (&c): " << &c << endl;
cout
<< "Value of c (c): " << c << endl << endl;

return 0;
}
Output
Address of c (&c): 0x7fff5fbff80c
Value of c (c): 5

Address that pointer pc holds (pc): 0x7fff5fbff80c
Content of the address pointer pc holds (*pc): 5

Address pointer pc holds (pc): 0x7fff5fbff80c
Content of the address pointer pc holds (*pc): 11

Address of c (&c): 0x7fff5fbff80c
Value of c (c): 2
Working of pointer in C++ programming
Explanation of program
  • When c = 5; the value 5 is stored in the address of variable c - 0x7fff5fbff8c.
  • When pc = &c; the pointer pc holds the address of c - 0x7fff5fbff8c, and the expression (dereference operator) *pc outputs the value stored in that address, 5.
  • When c = 11; since the address pointer pc holds is the same as c - 0x7fff5fbff8c, change in the value of c is also reflected when the expression *pc is executed, which now outputs 11.
  • When *pc = 2; it changes the content of the address stored by pc - 0x7fff5fbff8c. This is changed from 11 to 2. So, when we print the value of c, the value is 2 as well.

Comments

Popular posts from this blog

Momo suicide game

Microsoft clamps down on sick 'Momo suicide game' in 'Minecraft' Microsoft is clamping down on the sick “Momo suicide challenge,” which recently infiltrated the wildly popular online game “Minecraft.”The tech giant owns “Minecraft” developer Mojang. The vile “Momo suicide game” has been garnering attention after spreading on WhatsApp, prompting police warnings. "Momo" is a viral challenge that asks people to add a contact via WhatsApp - they are then   urged   to commit self-harm or suicide. The "game" has fueled comparisons to the sinister " Blue Whale challenge " that led to reports of suicides in Russia and the U.S, as well as the online fictional character of "Slender Man." In 2014 two 12-year-old girls in Wisconsin  attempted to kill   a classmate in an attempt to please the horror character. The Buenos Aires Times recently  reported  that police in Argentina are investigating whether “Momo” is linked to the suicide of a 12-y...

Spring Security with JWT for REST API

Spring is considered a trusted framework in the Java ecosystem and is widely used. It’s no longer valid to refer to Spring as a framework, as it’s more of an umbrella term that covers various frameworks. One of these frameworks is Spring Security , which is a powerful and customizable authentication and authorization framework. It is considered the de facto standard for securing Spring-based applications. Despite its popularity, I must admit that when it comes to single-page applications , it’s not simple and straightforward to configure. I suspect the reason is that it started more as an MVC application -oriented framework, where webpage rendering happens on the server-side and communication is session-based. If the back end is based on Java and Spring, it makes sense to use Spring Security for authentication/authorization and configure it for stateless communication. While there are a lot of articles explaining how this is done, for me, it was still frustrating to set it up for the f...

The Java 8 Stream API

1 Overview In this comprehensive tutorial, we'll go through the practical uses of Java 8 Streams from creation to parallel execution. To understand this material, readers need to have a basic knowledge of Java 8 (lambda expressions,   Optional,  method references) and of the Stream API. 2 Stream Creation There are many ways to create a stream instance of different sources. Once created, the instance  will not modify its source,  therefore allowing the creation of multiple instances from a single source. Empty Stream We should use the  empty()  method in case of the creation of an empty stream: Stream<String> streamEmpty = Stream.empty(); We often use the  empty()  method upon creation to avoid returning  null  for streams with no element: public Stream<String> streamOf (List<String> list) { return list == null || list.isEmpty() ? Stream.empty() : list.stream(); } 2.2. Stream of  Collection We can also creat...