×

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

Private Inheritance in C++

Private inheritance is an inheritance in object-oriented programming (OOP) languages where a subclass derives from a superclass. Still, the derived class does not inherit the public and protected members of...

7 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.

C++ Object Class

C++ Object Class Overview: C++ is a high-level programming language and an object-oriented programming language. An object-oriented language always has some properties of classes and objects. In this article, we...

4 minutes read.

Check for Balanced Brackets in an Expression (well-formedness) using Stack

Write a program that check the correctness of the pairs and ordering of the characters “{“, “}”, “(“, “)”, “[“, “]” in the expression string exp. Example: Checking for balanced parenthesis is one of...

2 minutes read.

Array of Vectors in C++ STL

Prerequisites: C++ STL Arrays and C++ STL Vector. A group of items kept in consecutive memory region is known as an array. It is to group similar objects of the same...

4 minutes read.

Pure Virtual Function in C++ With Example Program

What is a Virtual Function? A virtual function is created inside a class with the keyword virtual. A virtual function does not have any value to be returned. Once a virtual...

3 minutes read.

RTTI (Run-Time Type Information) in C++

In C++, RTTI or Run-Time Type Information reveals information about the data type of an object at runtime and only works with classes that have at least one virtual function....

3 minutes read.

C++ Prime number program

In this lesson, you'll learn how to verify whether a given number is a prime number or not in C++, and you'll obtain code to do it. What is the definition...

3 minutes read.

Diamond Pattern Using Do-While loop in C++

What is Do-While Loop? An iterative loop that checks the condition at the end.The Do-While loop can be used whenever a test condition is specific, as the control enters the loop...

5 minutes read.

Default arguments in C++

Arguments in a function are defined as the values supplied when the function is called. The source is the values supplied, and the destination is the receiving function. Let us...

3 minutes read.

C++ Writing to file

In file handling, write() function is used to write data into the file. The write() uses ofstream or fstream library to write into the file. Syntax file-stream-class   file-stream-object;   file-stream-object.write((char *)&var , sizeof (var)); The write() takes two arguments. The first argument is the address of variable var...

2 minutes read.

Object Slicing in C++

In this article, we will learn about Object slicing. When an object from a derived class is assigned to an object from a base class in C++, these extra attributes...

3 minutes read.

C++ Signal Handling

Signals are interruptions sent by the operating system to a process to cause it to cease doing its current job and focus on the task for which the interrupt was...

4 minutes read.

Call by Pointer in C++

What is Pointer? Every variable in C++ has a specific address or location in the computer's memory, and this address is known as the memory address. A pointer can be defined...

5 minutes read.

Binary Search in C++

The binary search in the C++ programming language will be discussed. By continually halves the array and then seeking specified items from a half array; binary search is a technique...

8 minutes read.

C++ First Program

Let's write a simple basic program structure of C++, its compilation and its execution (how it runs). This program is compiled using GCC compiler. Open any editor to write C++ program. #include<iostream>   using namespace std;   int main(){       cout<<"Welcome to C++ program"<<endl;   } Output...

2 minutes read.

For Loop Examples in C++

For Loop A for loop is a repetitive control structure that allows you to create a loop to execute a specific number of times efficiently. The syntax of for loop In C++, a...

6 minutes read.

C++ Fibonacci Series

What is a Fibonacci series? A Fibonacci series or sequence is a very popular programming paradigm. The next element occurring in the N terms series is determined by the sum of...

2 minutes read.

C++ Variable

In this article, we will discuss variables in C++ with their types and examples. What are Variables? Variables are specific memory storage spaces that hold a value. During the execution of a...

4 minutes read.

C++ Heap Sort

Heapsort is executed on the structure of the heap data. We know heap is a complete tree in binary form. The heap tree can be of two different types: Min-heap,...

3 minutes read.