DEV Community

Vijay Nerkar
Vijay Nerkar

Posted on

How to read pointers

Hi all,
This is my first post. Many peoples have problems on pointers.
This post will help to understand pointers.

  1. char a;
    Read : 'a' is a variable of char type.

  2. char *ptr;
    Read : 'ptr' is a pointer to char.

  3. char* ptr[5];
    Read : 'ptr' is an array of 5 pointers to a char.
    Note : The precedence rules say that the square brackets([]) are higher than the asterisk(*).
    Ex:
    int *ptr1 = NULL;
    int *ptr2 = NULL;
    int *ptr3 = NULL;
    int *ptr4 = NULL;
    int *ptr5 = NULL;
    char *ptr[5] = {ptr1, ptr2, ptr3, ptr4, ptr5};

  4. int ** ptr2;
    Read : 'ptr2' is a pointer to pointer to int
    Ex:

int a = 4;
int *ptr1 = &a;
int **ptr2 = &ptr1;

cout << "\n a : " << a;
cout << "\n Address of a : " << &a;
cout << "\n ptr1 : " << ptr1;
cout << "\n *ptr1 : " << *ptr1;
cout << "\n ptr2 : " << ptr2;
cout << "\n *ptr2 : " << *ptr2;
cout << "\n **ptr2 : " << **ptr2;

Output:

a : 4
Address of a : 0040F8D0
ptr1 : 0040F8D0
*ptr1 : 4
ptr2 : 0040F8C4
*ptr2 : 0040F8D0
**ptr2 : 4

  1. const int *ptr; Read : 'ptr' is a pointer to const int Ex: const int a = 4; const int *ptr = &a;

cout << "\n a : " << a;
cout << "\n Address of a : " << &a;
cout << "\n ptr : " << ptr;
cout << "\n *ptr : " << *ptr;

Output:

a : 4
Address of a : 0040F8D0
ptr : 0040F8D0
*ptr : 4

  1. const int **ptr2; Read : 'ptr2' is a pointer to pointer to const int Ex: const int a = 4; const int *ptr1 = &a; const int **ptr2 = &ptr1;

cout << "\n a : " << a;
cout << "\n Address of a : " << &a;
cout << "\n ptr1 : " << ptr1;
cout << "\n *ptr1 : " << *ptr1;
cout << "\n ptr2 : " << ptr2;
cout << "\n *ptr2 : " << *ptr2;
cout << "\n **ptr2 : " << **ptr2;

Output:

a : 4
Address of a : 0040F8D0
ptr1 : 0040F8D0
*ptr1 : 4
ptr2 : 0040F8C4
*ptr2 : 0040F8D0
**ptr2 : 4

  1. char * const * ptr2; Read : ptr2 is a pointer to const pointer to char Ex: char a = 'x'; char *const ptr1 = &a; char * const * ptr2 = &ptr1;

cout << "\n a : " << a;
cout << "\n Address of a : " << &a;
cout << "\n ptr1 : " << ptr1;
cout << "\n *ptr1 : " << *ptr1;
cout << "\n ptr2 : " << ptr2;
cout << "\n *ptr2 : " << *ptr2;
cout << "\n **ptr2 : " << **ptr2;

Output:

a : x
Address of a : 0040F8D0
ptr1 : 0040F8D0
*ptr1 : x
ptr2 : 0040F8C4
*ptr2 : 0040F8D0
**ptr2 : x

  1. const int * const * ptr2; Read : ptr2 is a pointer to const pointer to const int Ex: const int a = 10; const int * ptr1 = &a; const int * const * ptr2 = &ptr1;

printf("\n a : %d ", a);
printf("\n Address of a : %d ", &a);
printf("\n ptr1 : %d ", ptr1);
printf("\n ptr1 : %d ", ptr1);
printf("\n ptr2 : %d ", ptr2);
printf("\n *ptr2 : %d ", *ptr2);
printf("\n **ptr2 : %d ", **ptr2);

Output :
a : 10
Address of a : 4456124
ptr1 : 4456124
*ptr1 : 10
ptr2 : 4456112
*ptr2 : 4456124
**ptr2 : 10

  1. int ** const ptr2; Read : ptr2 is a const pointer to pointer to int Ex: int a = 10; int * ptr1 = &a; int ** const ptr2 = &ptr1;

printf("\n a : %d ", a);
printf("\n Address of a : %d ", &a);
printf("\n ptr1 : %d ", ptr1);
printf("\n ptr1 : %d ", ptr1);
printf("\n ptr2 : %d ", ptr2);
printf("\n *ptr2 : %d ", *ptr2);
printf("\n **ptr2 : %d ", **ptr2);

Output :
a : 10
Address of a : 4456124
ptr1 : 4456124
*ptr1 : 10
ptr2 : 4456112
*ptr2 : 4456124
**ptr2 : 10

  1. const int ** const ptr2; Read : ptr2 is a const pointer to pointer to const int Ex: const int a = 10; int * ptr1 = &a; const int ** const ptr2 = &ptr1;

printf("\n a : %d ", a);
printf("\n Address of a : %d ", &a);
printf("\n ptr1 : %d ", ptr1);
printf("\n ptr1 : %d ", ptr1);
printf("\n ptr2 : %d ", ptr2);
printf("\n *ptr2 : %d ", *ptr2);
printf("\n **ptr2 : %d ", **ptr2);

Output :
a : 10
Address of a : 4456124
ptr1 : 4456124
*ptr1 : 10
ptr2 : 4456112
*ptr2 : 4456124
**ptr2 : 10

  1. int * const * const ptr2; Read : ptr2 is a const pointer to const pointer to int Ex: int a = 10; const int * ptr1 = &a; int * const * const ptr2 = &ptr1;

printf("\n a : %d ", a);
printf("\n Address of a : %d ", &a);
printf("\n ptr1 : %d ", ptr1);
printf("\n ptr1 : %d ", ptr1);
printf("\n ptr2 : %d ", ptr2);
printf("\n *ptr2 : %d ", *ptr2);
printf("\n **ptr2 : %d ", **ptr2);

Output :
a : 10
Address of a : 4456124
ptr1 : 4456124
*ptr1 : 10
ptr2 : 4456112
*ptr2 : 4456124
**ptr2 : 10

  1. const int * const * const ptr2; Read : ptr2 is a const pointer to const pointer to const int Ex : const int a = 10; const int * ptr1 = &a; const int * const * const ptr2 = &ptr1;

printf("\n a : %d ", a);
printf("\n Address of a : %d ", &a);
printf("\n ptr1 : %d ", ptr1);
printf("\n ptr1 : %d ", ptr1);
printf("\n ptr2 : %d ", ptr2);
printf("\n *ptr2 : %d ", *ptr2);
printf("\n **ptr2 : %d ", **ptr2);

Output :
a : 10
Address of a : 4456124
ptr1 : 4456124
*ptr1 : 10
ptr2 : 4456112
*ptr2 : 4456124
**ptr2 : 10

Pointer Arithmatic :

  1. // i is a integer constant which contains value 10 const int i=10;

// ptr is a pointer to integer constant
const int *ptr = &i;

(*ptr)++;//error
Because trying to modify value of i but i is a const variable.

ptr++;//allowed
Here we incrementing address pointer by ptr by 1. Means suppose address of a is 100.
Now ptr contains 100(address of i)
ptr++ just incrementing 100 to 104. Now if we print value at address 104 ,
it will be print some garbage value.

  1. // arr is an array of six integers out of which three are initialized int arr[6]={1,2,3}; // ptr is a pointer to integer which pointing at base address of array arr int *ptr = arr; ptr++;//allowed (*ptr)++;//allowed

Ex:
int arr[6] = {11,22,33};
int *ptr = arr;
printf("\n *ptr : %d ", *ptr);
ptr++;
printf("\n *ptr : %d ", *ptr);
(*ptr)++;
printf("\n *ptr : %d ", *ptr);

Output:
*ptr : 11
*ptr : 22
*ptr : 23

3.// arr is an array of three integer constant
int const arr[3]={1,2,3};

// ptr1 is a pointer to integer which pointing at base address of array arr
int *ptr1 = arr;
ptr1++;//allowed
(*ptr1)++;//allowed
(arr[2])++;//error

Ex:
int arr[] = {12,33,50};
int *ptr1 = arr;
int i = 0;

for (i = 0; i < 3; i++)
{
printf("\nArr[%d] : %d", i,arr[i]);
}

printf("\n ptr1 : %d ", *ptr1);
ptr1++;
printf("\n *ptr1 : %d ",
ptr1);
arr[1]++;
printf("\n arr[1] : %d ",arr[1]);

Output:
Arr[0] : 12
Arr[1] : 33
Arr[2] : 50

*ptr1 : 12
*ptr1 : 33
arr[1] : 34


Enter fullscreen mode Exit fullscreen mode

Top comments (0)