5.3) Types of Pointers in C
Table of Contents
1. Null Pointer
A null pointer is a pointer that doesn’t point to any valid memory address.
It’s often used to indicate that a pointer doesn’t currently refer to any data.
Example: Using Null Pointer
#include <stdio.h>
int main() {
int *ptr = NULL;
if (ptr == NULL) {
printf("Pointer is NULL\n");
} else {
printf("Pointer is not NULL\n");
}
return 0;
}
Explanation:
- In this example, the pointer
ptris assigned the valueNULL. - By convention, you should always initialize pointers to
NULLwhen they are declared. - Using conditional statements, you can check whether a pointer is
NULLbefore accessing its value.
2. Void Pointer (Generic Pointer):
A void pointer is a special type of pointer that doesn’t have a specific data type associated with it. It can point to any data type.
Example: Using Void Pointer
#include <stdio.h>
int main() {
int num = 42;
float pi = 3.14;
void *ptr;
ptr = #
printf("Value of num: %d\n", *(int *)ptr);
ptr = π
printf("Value of pi: %.2f\n", *(float *)ptr);
return 0;
}
Explanation:
- In this example, the void pointer
ptris used to point to both an integer (num) and a float (pi). - To dereference a void pointer, you need to cast it to the appropriate data type.
- Void pointers are useful when you need to create functions that can handle different data types.
3. Function Pointer
A function pointer is a pointer that points to a function instead of a variable.
It allows you to dynamically call functions at runtime.
Example: Using Function Pointer
#include <stdio.h>
void greet() {
printf("Hello, World!\n");
}
int main() {
void (*ptr)() = greet; // Declare a function pointer
ptr(); // Call the function using the pointer
return 0;
}
Explanation:
- In this example, a function
greetis defined to print a greeting. - A function pointer
ptris declared to point to thegreetfunction. - The function is called using the pointer
ptr().
4. Pointer to Pointer (Double Pointer)
A pointer to a pointer is a pointer that points to the memory address of another pointer.
It’s often used in scenarios involving dynamic memory allocation.
Example: Using Pointer to Pointer
#include <stdio.h>
int main() {
int num = 42;
int *ptr = #
int **ptr_to_ptr = &ptr;
printf("Value of num: %d\n", **ptr_to_ptr);
return 0;
}
Explanation:
- In this example, a pointer
ptrpoints to an integernum. - A pointer to pointer
ptr_to_ptrpoints to the memory address of theptrpointer. - To access the value of
num, you need to dereference both pointers using**ptr_to_ptr.
5. Constant Pointers:
A constant pointer is a pointer whose value (memory address) cannot be changed once it’s initialized. However, the value it points to can be modified.
Example: Using Constant Pointer
#include <stdio.h>
int main() {
int num = 42;
int *const ptr = #
*ptr = 99; // Modifying the value it points to is allowed
// ptr = &new_num; // Error: Cannot change the value of ptr
printf("Modified value of num: %d\n", num);
return 0;
}
Explanation:
- In this example, a constant pointer
ptris declared and initialized to point to an integernum. - The value of
numcan be modified through the constant pointer, but you cannot change the memory address thatptrpoints to.
Of course! Let’s delve further into types of pointers in C and cover more important points:
6. Pointer to Constant
A pointer to constant is a pointer that points to a constant value. It can’t be used to modify the value it points to, but it can be used to point to different constant values.
Example: Using Pointer to Constant
#include <stdio.h>
int main() {
const int num = 42;
const int *ptr = #
// *ptr = 99; // Error: Cannot modify the value it points to
// ptr = &new_num; // Valid: Can point to a different constant
printf("Value of num: %d\n", *ptr);
return 0;
}
Explanation:
- In this example, a pointer to a constant
ptris declared and initialized to point to a constant integernum. - The value of
numcannot be modified through the pointer, but the pointer itself can be changed to point to different constants.
7. Constant Pointer to Constant
A constant pointer to constant is a pointer that can’t be used to modify the value it points to, and it also can’t be used to change the memory address it points to.
Example: Using Constant Pointer to Constant
#include <stdio.h>
int main() {
const int num = 42;
const int *const ptr = #
// *ptr = 99; // Error: Cannot modify the value it points to
// ptr = &new_num; // Error: Cannot change the memory address it points to
printf("Value of num: %d\n", *ptr);
return 0;
}