×

C++ Pointer

Pointer is a derived data type that stores the address of a variable. A pointer is used for memory management and dynamic memory allocation. Pointer works on the address of data rather than actual of data.

Pointer Declaration

data-type *pointer-variable;
Or
data-type * pointer-varaible;
Or
data-type* pointer-varaible;
Here data-type is one of the C++ data types, pointer-variable is the name of pointer and * (asterisk) refers to the pointer. Asterisk (*) can be placed at three different locations as:
  1. Place immediately before pointer-variable.
  2. Place between the data type and the pointer variable.
  3. Place immediately after the data type.
Example
int *ptr;  
char *ch;

 

Pointer Initialization

pointer-variable = &variable;
Example
int *ptr,x;  
ptr = &x; //initialize
Here ptr is a pointer variable, x is variable of int data type. &x contains the address of variable x and prt = &x; assign address of x into ptr.

 

Deference operator (*) and Reference operator (&)

Deference operator (*) gives the value of variable stored at the memory location. Reference operator (&) gives the memory address of a variable. For example: If a number variable is stored in the memory address 0x22, and it contains a value 10. Dereference (*) operator gives the value 10, while reference (&) operator gives the value 0x22.

C++ Pointer Example

#include <iostream>  
using namespace std;  
int main() {  
    int *ptr, x;  
    x = 10;  
    ptr=&x;  
    cout << "Value of x (x): " << x << endl;  
    cout << "Value of x at address ptr(*ptr): " << *ptr << endl;  
    cout << "Address of x (&x): " << &x << endl;  
    cout << "Address of x that ptr contain (ptr): " << ptr << endl;  
    *ptr = 5;   
    cout << "Address of x (&x): " << &x << endl;  
    cout << "Value of x (x): " << x << endl;  
    return 0;  
}
Output:
Value of x (x): 10
Value of x at address ptr(*ptr): 10
Address of x (&x): 0x22fe34
Address of x that ptr contain (ptr): 0x22fe34
Address of x (&x): 0x22fe34
Value of x (x): 5

Related Topics

Differences between #define & const in C/C++

 Differences between #define & const in C/C++ A preprocessor directive is #define. The preprocessor replaces things defined by #define prior to starting compilation. In this chapter, we'll learn about the member, variable,...

3 minutes read.

Precision of floating point numbers Using these functions floor(), ceil(), trunc(), round() and setprecision() in C++

Precision of floating point numbers Using these functions floor(), ceil(), trunc(), round() and setprecision() in C++ 1/2 decimal equal is 0.555555555555555555555 .... An indefinite number of lengths will require the storage...

4 minutes read.

Classes and Objects in C++

When it comes to object-oriented programming, objects are the basic building blocks. Memory is taken up by objects, which contain data and methods or functions that operate on it. On...

3 minutes read.

C++ Break

In this article, we will discuss the C++ Break statement with its syntax, algorithm, pseudocode, and examples. The C++ break statement also terminates the currently active loop or switch statement immediately....

4 minutes read.

Constructor Overloading

The program contains more than one constructor in a class with the same name, and different types of arguments are called constructor overloading. Calling of constructor depends on the number and types...

2 minutes read.

Pointer to Object in C++

What is a pointer? A pointer in C++ is used to point the variable by storing the address of the variable. In C++, to print the address of the variable, we...

4 minutes read.

Dynamic Constructor in C++

Dynamic constructors create dynamic memory by using a dynamic memory allocator new within the constructor. This allows us to initialize objects dynamically. The term dynamic constructor is used when memory...

2 minutes read.

C++ Program For FCFS (First Come First Serve)

The most basic scheduling technique is FCFS, often known as "FIFO (First In, First Out)". In this procedure, the first one is utilized and executed first, while the second one...

4 minutes read.

C++ Program: Matrix Multiplication

Matrix Multiplication in C++ What is a Matrix? A matrix is a set of numbers in the form of rows and columns forming a rectangular array. It includes numbers, which are often...

4 minutes read.

C++ Ternary Operator

In this tutorial, we'll learn about the C++ ternary operator and how to utilise it to manage the program's flow using examples. Ternary Operator: The if-else statement and the conditional operator use...

3 minutes read.

C++ Installation

Let's install C++ setup to start programming in C++. C++ setup contains C++ compiler which is required in your system. There are lots of C++ compilers available, you must choose...

1 minute read.

Armstrong Number using While Loop in C++

What is while Loop? A while loop or while statement repeats all code of its body as long as a specific condition is satisfied. The loop ends if or when the...

4 minutes read.

C++ Nested if

C++'s nested if statements enable more complex decision-making when a section of code needs to execute only after a set of conditions is met. The nested if control statement refers...

4 minutes read.

C++ Enumeration

C++ Enumeration In C++, Enum is a special data type that contains some fixed sets of components that have various applications in the programming. Enum works fine with fixed constant sets...

3 minutes read.

Abstract class in C++

In this article, you will get exposure to an abstract class in C++. We will discuss this topic using some practical examples too. To understand the abstract classes, you should...

6 minutes read.

Continue in C++ While loop

Continue statement: Inside the loop, the continue statement is used to control the loop. C++ utilizes the continue keyword to implement the continue statement, which transfers the program's flow at the...

4 minutes read.

Message Passing in C++

The act of sending and receiving information by an object is referred to as communication, and all communication between objects that takes place via message is known as message passing....

1 minute read.

Inheritance Program in C++

What is Inheritance? Inheritance is the ability of a class to inherit traits and properties from another class. One of the most crucial aspects of Object-Oriented Programming is inheritance. The ability or...

9 minutes read.

C++ Forward Iterators

Iterators : Iterators serve as a link between algorithms and STL containers, allowing the data inside the container to be modified. They let you to iterate through the container, access and...

3 minutes read.

Bits stdc++.h in C++

<bits/stdc++.h> in C++ In essence, it is a header file that contains all the standard libraries. It makes sense to use this file in programming competitions to speed up work, especially...

2 minutes read.