In this article, we are going to learn types of pointers
Advanced Pointer Notations:
There are some other ways where pointers get in handy.
Pointers with multidimensional arrays:
Pointers can also be used with multidimensional arrays.
For example, look at the below multi-dimensional array sample.
Consider pointer notation for the two-dimensional numeric arrays. consider the following declaration
int nums[2][3] = { { 16, 18, 20 }, { 25, 26, 27 } };
In general, nums[ i ][ j ] is equivalent to *(*(nums+i)+j)
This is how pointers can be operated in multi-dimensional arrays:
Pointers with strings
Strings are nothing but a collection of characters in contiguous memory locations. Hence, they can be considered as an array of characters with a null character at the end.
We can use the pointers with string literals in the following way:
const char * ptr = "geek";
This declares an array with the literal representation for “geek”, and then a pointer to its first element is assigned to ptr. If we imagine that “geek” is stored at the memory locations that start at address 1800, we can represent the previous declaration as:
As pointers and arrays behave the same in the case of expressions, pointers can be used to access the string literals like this:
char x = *(ptr+3);
char y = ptr[3];
Here, both x and y contain k stored at 1803 (1800+3).
TYPES OF POINTERS:
Pointers to Pointers:
In c++, we have the privilege to store the address of one pointer in another pointer and can easily dereference it.
This is not present in C and introduced in c++.
In C++, we can create a pointer to a pointer that in turn may point to data or another pointer. The syntax simply requires the unary operator (*) for each level of indirection while declaring the pointer.
char a;
char *b;
char ** c;
a = ’g’;
b = &a;
c = &b;
In this example:
a stores value of g
b stores address of a
C stores address of b
VOID POINTERS
Void pointers are a special type of pointers used to represent the absence of type. void pointers are pointers that have no type. Hence, their length is undetermined and de-referencing is undetermined.
Void pointers have great flexibility but they have to transform into another type before de-referencing them.
Now, let’s see an example to implement the void pointers:
// C++ program to illustrate Void Pointer in C++
#include <bits/stdc++.h>
using namespace std;
void increase(void *data,int ptrsize)
{
if(ptrsize == sizeof(char))
{
char *ptrchar;
//Typecast data to a char pointer
ptrchar = (char*)data;
//Increase the char stored at *ptrchar by 1
(*ptrchar)++;
cout << "*data points to a char"<<"\n";
}
else if(ptrsize == sizeof(int))
{
int *ptrint;
//Typecast data to an int pointer
ptrint = (int*)data;
//Increase the int stored at *ptrchar by 1
(*ptrint)++;
cout << "*data points to an int"<<"\n";
}
}
void dec()
{
// Declare a character
char c='x';
// Declare an integer
int i=10;
//Call increase function using a char and int address respectively
increase(&c,sizeof(c));
cout << "The new value of c is: " << c <<"\n";
increase(&i,sizeof(i));
cout << "The new value of i is: " << i <<"\n";
}
//Driver program
int main()
{
dec();
}
OUTPUT
*data points to a char
The new value of c is: y
*data points to an int
The new value of i is: 11
In the above example, we created a function that will take the reference of a variable into the void pointer and its corresponding size which helps in transforming the pointer and operating it.
That’s it for this article. In the next article, we will learn about the other types of pointers.