Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Data type of a Pointer in C++

  • Last Updated : 05 Jan, 2022

A pointer is a variable that stores the memory address of an object.  The pointer then simply “points” to the object. The type of the object must correspond with the type of the pointer. Pointers are used extensively in both C and C++ for three main purposes:

  • To allocate new objects on the heap.
  • To pass functions to other functions.
  • To iterate over elements in arrays or other data structures.

Pointers are used to store and manage the addresses of dynamically allocated blocks of memory. Such blocks are used to store data objects or arrays of objects. Most structured and object-oriented languages provide an area of memory, called the heap or free store, from which objects are dynamically allocated.

Data Type of Pointer

Pointer is a data type and the purest form of it in C is void *. A void * can pass the memory address around which is what a pointer does but it cannot be dereferenced. 

Dereferencing means to get at the data contained at the memory location the pointer is pointing at. This means that one will know what type of data will be read from the memory.

Since void is nothing, it means that it can point to any block of memory, that contains any type of data. void * is rather a restricted type as one cannot dereference it and thus it cannot be used for pointer arithmetic. 

To make life easier C provides a series of derived pointer types like char *, int *, double *, and so on. Pointers have their own size irrespective of the type of data they are pointing to.

Example:

char *x – pointer to char

int *x – pointer to integer

C program to print the size of the pointer:

C




// C program to print the
// size of pointer
#include <stdio.h>
  
// Driver code
int main()
{
    int x = 10;
    int* ptr = &x;
  
    // Print size of variable
    printf("Size of variable: %zu",
            sizeof x);
  
    // Print size of pointer
    printf("\nSize of pointer: %zu",
            sizeof ptr);
  
    return 0;
}
Output
Size of variable: 4
Size of pointer: 8

C++ program to print the address of the pointer:

C++




// C++ program to point address
// of a pointer
#include <iostream>
using namespace std;
  
// Driver code
int main()
{
    int *ptr, var;
    var = 5;
  
    // Assign address of var
    // to ptr
    ptr = &var;
  
    // Access value pointed by ptr
    cout << "Value pointed by ptr: " << 
            *ptr << endl;
  
    // Address stored by ptr
    cout << "Address stored at ptr: " << 
             ptr << endl;
  
    // Address of pointer ptr
    cout << "Address of ptr: " << 
             &ptr;
}
Output
Value pointed by ptr: 5
Address stored at ptr: 0x7ffe1c19b10c
Address of ptr: 0x7ffe1c19b110

Why declaring data type of pointer is necessary?

The data type of a pointer is needed in two situations:

  1. Dereferencing the pointer.
  2. Pointer arithmetic.

Let’s discuss each of these in detail.

Dereferencing the pointer:

The data type of pointer is needed when dereferencing the pointer so it knows how much data it should read. For example, dereferencing a char pointer should read the next byte from the address it is pointing to, while an integer pointer should read 4 bytes.

Below is the C program to demonstrate the dereferencing an int pointer

C




// C program to demonstrate
// dereferencing int pointer
#include <stdio.h>
int main()
{
    // Declare the integer variable
    int a = 20;
  
    // Declare the integer pointer
    // variable.
    int* ptr;
  
    // Store the address of 'x' variable
    // to the pointer variable 'ptr'
    ptr = &a;
  
    // value of 'x' variable is changed
    // to 8 by dereferencing a pointer 'ptr'
    *ptr = 8;
    printf("Value of x is : %d", a);
    return 0;
}

Pointer arithmetic:

Different types of pointers take different amounts of memory. So, in the case of advancing a pointer one needs to take the type’s size into the account.

Example:

char takes 1 byte
char  c = ‘a’;
char *ptr = &c;
ptr++;
Here, ptr++ means ptr = ptr + 1 as char takes 1 byte. 
This means adding 0x01 to the address.

Similarly, for int it is 4 bytes, so ptr++ in case of int will be adding 0x04 to the address stored in the pointer.

Pointer data type is a special kind of variable which are meant to store addresses only, instead of values(integer, float, double, char, etc).  It knows how many bytes the data is stored in. When we increment a pointer, we increase the pointer by the size of the data type to which it points. 
Let’s consider the following memory block-

Memory Block having 1byte, 4byte, 8byte and 64byte blocks

A pointer takes 8 bytes of memory to get stored. In the above memory block-

  1. If the pointer ‘ptr’ is stored at Point B (address between 1000000-10000099) with which pointer type is not declared (i.e. it will get stored in any available spot) and originally the address it is pointing to a variable which lies between 2100-2199 then it will take a lot of time to access the variable it is pointing to.
  2. If the pointer is declared with data type then it gets stored to nearest available position to that value like getting stored at Point A (address 2200-2299) which is nearest to ‘int’ type variables i.e. 4-byte row so the access would also be faster in comparison. 

Therefore, it can be concluded that the main reason behind declaring pointers with data type is it can help with saving extra time spend in access of variables during its implementation.

Significance of declaring data type of pointer:

  1. Without data type safety cannot be assured.
  2. Declaring data type helps to increase the speed of access to the variable pointer is pointing to.
  3. The compiler has to know the size of the memory cell, the pointer is pointing to.
  4. Typecasting of pointer is a must when accessing structures from the pointer.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!