Data type of a Pointer in C++
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.
char *x – pointer to char
int *x – pointer to integer
C program to print the size of the pointer:
Size of variable: 4 Size of pointer: 8
C++ program to print the address of the pointer:
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:
- Dereferencing the pointer.
- 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
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.
char takes 1 byte
char c = ‘a’;
char *ptr = &c;
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-
A pointer takes 8 bytes of memory to get stored. In the above memory block-
- 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.
- 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:
- Without data type safety cannot be assured.
- Declaring data type helps to increase the speed of access to the variable pointer is pointing to.
- The compiler has to know the size of the memory cell, the pointer is pointing to.
- Typecasting of pointer is a must when accessing structures from the pointer.