Types of Pointers in C
Pointer Definition In C Programming
A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before using it to store any variable address. The general form of a pointer variable declaration is −
What is a pointer in programming?
- In computer science, a pointer is a programming language object, whose value refers to (or "points to") another value stored elsewhere in the computer memory using its memory address. A pointer references a location in memory, and obtaining the value stored at that location is known as dereferencing the pointer.
- A pointer is a variable which contains the address in memory of another variable. We can have a pointer to any variable type.
- The unary or monadic operator & gives the "address of a variable''.
- The indirection or dereference operator * gives the "contents of an object pointed to by a pointer''.
The general form of a pointer variable declaration is −
Here, the type is the pointer's base type; it must be a valid C data type, and var-name is the name of the pointer variable. The asterisk * used to declare a pointer is the same asterisk used for multiplication. However, in this statement, the asterisk is being used to designate a variable as a pointer. Take a look at some of the valid pointer declarations −
int *ip; /* pointer to an integer */
double *dp; /* pointer to a double */
float *fp; /* pointer to a float */
char *ch /* pointer to a character */
What is a pointer in C programming?
Pointer is a user defined data type which creates special types of variables which can hold the address of primitive data type like char, int, float, double or user defined data type like function, pointer, etc. or derived data type like an array, structure, union, enum.
Types Of Pointers In C Programming
- NULL Pointer
- Dangling Pointer
- Generic Pointers
- Wild Pointer
- Complex Pointers
- Near Pointer
- Far Pointer
- Huge Pointers
List Of Pointers In C Programming
1. Null Pointer
- NULL Pointer is a pointer which is pointing to nothing.
- NULL pointer points the base address of the segment.
- In case, if you don’t have address to be assigned to pointer then you can simply use NULL
- Pointer which is initialized with NULL value is considered as a NULL pointer.
- NULL is macro constant defined in following header files –
Defining NULL Value
#define NULL 0
Below are some of the variable representations of a NULL pointer.
float *ptr = (float *)0;
char *ptr = (char *)0;
double *ptr = (double *)0;
char *ptr = '\0';
int *ptr = NULL;
Example of NULL Pointer
int *ptr = NULL;
printf("The value of ptr is %u",ptr);
The value of ptr is 0
2. Dangling Pointer
- Dangling pointers arise when an object is deleted or de-allocated, without modifying the value of the pointer, so that the pointer still points to the memory location of the de-allocated memory.
- In short, pointer pointing to a non-existing memory location is called dangling pointer.
Examples of Dangling Pointer
There are different ways where Pointer acts as a dangling pointer.
Way 1 : Using free or de-allocating memory
char *ptr = malloc(Constant_Value);
free (ptr); /* ptr now becomes a dangling pointer */
We have declared the character pointer in the first step. After execution of some statements, we have the de-allocated memory which is allocated previously for the pointer.
As soon as memory is de-allocated for pointer, pointer becomes dangling pointer
Way 2 : Out of Scope
char *ptr = NULL;
ptr = &ch;
..... /* dp is now a dangling pointer */
- Character Pointer is Declared in the first Step.
- Pointer Variable ‘ptr’ is pointing to Character Variable ‘ch’ declared in the inner block .
- As character variable is non-visible in Outer Block, then Pointer is Still Pointing to Same Invalid memory location in Outer block, then Pointer becomes “Dangling”.
3. Generic Pointers
When a variable is declared as being a pointer to type void, it is known as a generic pointer. Since you cannot have a variable of type void, the pointer will not point to any data and therefore cannot be dereferenced. It is still a pointer though, to use it you just have to cast it to another kind of pointer first. Hence the term Generic pointer.
This is very useful when you want a pointer to point to data of different types at different times.
Example of Generic Pointer
Here is some code using a void pointer:
i = 6;
c = 'a';
the_data = &i;
printf("the_data points to the integer value %d\n", *(int*) the_data);
the_data = &c;
printf("the_data now points to the character %c\n", *(char*) the_data);
4. Wild Pointer
A Pointer in C that has not been initialized till its first use is known as the Wild pointer. A wild pointer points to some random memory location.
Example of Wild Pointer
/* Ptr is a wild pointer, as it is not initialized Yet */
How can we avoid Wild Pointers ?
We can initialize a pointer at the point of declaration wither by the address of some object/variable or by NULL;
int val = 5;
int *ptr = &val; /* Initializing pointer */
/* Ptr is not a wild pointer, it is pointing to the address of variable val */
5. Complex Pointers
Precedence : Operator precedence describes the order in which C reads expressions
Associativity : Order operators of equal precedence in an expression are applied
We need to assign the priority to the pointer declaration considering precedence and associative according to the following table.
(): This operator behaves as bracket operator or function operator.
: This operator behaves as array subscription operator.
*: This operator behaves as pointer operator, not as the multiplication operator.
Identifier: It is not an operator, but it is name of the pointer variable. You will always find the
Priority will be assigned to the name of the pointer.
Data type: It is also not an operator. Data types also include modifier (like signed int,
long double etc.)
6. Near Pointer
- The pointer which can points only 64KB data segment or segment number 8 is known as near pointer.
- That is near pointer cannot access beyond the data segment like graphics video memory, text video memory, etc. Size of near pointer is two byte. With the help of keyword near, we can make any pointer as near pointer.
Example of Near Pointer
int near* ptr;
7. Far Pointer
- The pointer which can point or access whole the residence memory of RAM, i.e., which can access all 16 segments is known as far pointer.
- Size of the far pointer is 4 byte or 32 bit.
Example of Far Pointer
int far *ptr;
Output : 4
8. Huge Pointer
- The pointer which can point or access whole the residence memory of RAM i.e. which can access all 16 segments is known as a huge pointer.
- Size of the far pointer is 4 byte or 32 bit.
Example of Huge Pointer
char huge * far *p;
printf("%d %d %d",sizeof(p),sizeof(*p),sizeof(**p));
Output : 4 4 1
Explanation: p is the huge pointer, *p is the far pointer and **p is char type data variable.
Advantages of Pointers In C Programming
- We can dynamically allocate or deallocate space in memory at a run time by using pointers.
- Using pointers, we can return multiple values from a function.
- We can pass arrays to a function as call by Reference.
- Pointers are used to efficiently access array elements, as array elements are stored in adjacent memory locations. If we have a pointer pointing to a particular element of an array, then we can get the address of next element by simply incrementing the pointer.
- Pointers in C are used to efficiently implement dynamic Data Structures like Queues, Stacks, Linked Lists, Trees, etc.
- The use of pointers results in the faster execution of the program.
For in-depth understanding of Pointers click on