Photo of Hyacinth macaw by Roi Dimor on Unsplash
Types of Pointers in C / C++
1. Null Pointer
It is a pointer pointing to nothing. NULL pointer points to the base address of the segment.
-EXAMPLE-
int * ptr = (int) * 0;
float * fptr = (float) * 0;
double * dptr = (double) * 0;
char * chptr = (char) * 0;
Other ways of initializing NULL pointer
int * ptr = NULL;
char * chptr = '\0';
NULL also means 0 in macro
#define NULL 0
2. Dangling Pointer
A pointer pointing to the memory address of any variable (or object) which has been deleted from memory.
When a pointer points to a deleted memory address, the pointer is called as a dangling pointer.
-EXAMPLE-
Person p = new Person();
Person * pptr = &p;
delete p;
/* here pointer pptr (* pptr) is still pointing to
the Person object which has been deleted.
*/
3. Generic Pointer
void pointer is known as generic pointer. It can point to any type of data.
-EXAMPLE-
void * ptr;
Points to remember
- We can’t de-reference a generic pointer.
- We can find the size of pointer using
sizeof()
operator. - These pointers can hold any type of pointer such as char, int, float, structure, array, object etc without any typecasting.
- Any type of pointer can hold generic pointer without typecasting.
- Generic pointer can be used to implement dynamic datatype.
4. Wild Pointer
A pointer which has not been initialized is called as wild pointer.
-EXAMPLE-
int * ptr;
5. Complex Pointers
These are pointers pointing to derived data-types.
- Pointer to array
- Pointer to function
- Pointer to structure / union
- Pointer to object
- Multilevel pointers
6. near / far / huge Pointers
These pointer modifiers were used in 1980’s-90’s before 32 bit architecture.
near : a 16 bit pointer that can address any byte in a 64k segment.
far : a 32 bit pointer that contains a segment and an offset.
huge : a 32 bit pointer in which the segment is “normalized” so that no two far pointers point to the same address unless they have the same value.
You can read this stack overflow answer for more information.
Thanks for reading. 🙏
If you liked it feel free to share with your dear ones.💗
And tell me what do you think in the comment box.💬
Stay tuned.
comments powered by Disqus