C++ POINTER

  • A pointer is a particular variable that holds the memory  address and usually holds the location of another variable in the memory
  • This variable can be of type int, char, array, function, or any other pointer. The size of the pointer depends on the architecture
  • However, in 32-bit architecture the size of a pointer is 2 byte.

Consider the following example to define a pointer that stores the address of an integer.

int n = 10;
int* p = &n; // Variable p of type pointer is pointing to the address of the variable n of type integer

DECLARING POINTER

The pointer in the c language can be declared using * (asterisk symbol). It is also known as an indirection pointer used to dereference a pointer.

int *a;//pointer to int
char *c;//pointer to char  

EXAMPLE

#include<iostream>
using namespace std;
int main(){
int number=50;
int *p;
p=&number;//stores the address of number variable
cout<<Address of p variable is %x n",p; // p contains the address of the number therefore printing p gives the address of number.
cout<<Value of p variable is %d n",*p; // As we know that * is used to dereference a pointer therefore if we print *p, we will get the value stored at the address contained by p.
return 0;
}    

OUTPUT

Address of number variable is fff4
Address of p variable is fff4
Value of p variable is 50

POINTERS TO ARRAY

int arr[10];
int *p[10]=&arr; // Variable p of type pointer is pointing to the address of an integer array arr.  

POINTERS TO FUNCTION

void show (int);
void(*p)(int) = &display; // Pointer p is pointing to the address of a function  

POINTER TO STRUCTURE

struct st {
    int i;
    float f;
}ref;
struct st *p = &ref;  
USAGE OF POINTER

There are many applications of pointers in the c language.

1) Dynamic memory allocation

In c language, we can dynamically allocate memory using malloc() and calloc() functions where the pointer is used.

2) Arrays, Functions, and Structures

Pointers in c language are widely used in arrays, functions, and structures. It reduces the code and improves performance.

NULL POINTER
  • A pointer that is not assigned any value but NULL is known as the NULL pointer.
  • If you don’t have any address to be specified in the pointer at the time of declaration, you can assign NULL value.
  • It will provide a better approach.
int *p=NULL;

In most libraries, the value of the pointer is 0 (zero).

READING COMPLEX POINTERS

  • There are several things that must be taken into consideration while reading the complex pointers in C.

  • Let’s see the precedence and associativity of the operators which are used regarding pointers.

OPERATOR

(), []

*, identifier

Data type

PRECEDENCE

1

2

3

ASSOCIATIVITY

Left to Right

Right to Left

Here, we must notice that

  • (): This operator is a bracket operator used to declare and define the function.
  • []: This operator is an array subscript operator
  • * : This operator is a pointer operator.
  • Identifier: It is the name of the pointer. The priority will always be assigned to this.
  • Data type: Data type is the type of the variable to which the pointer is intended to point. It also includes modifiers like signed int, long, etc).