In this tutorial, you will learn about pointer compatibility in C. We’ll explain when two pointers are considered compatible or not. You’ll also see how C treats different pointer types. Along the way, we’ll highlight common mistakes and how to avoid them.
What is Pointer Compatibility?
In C, pointer compatibility refers to the rules governing the assignment and comparison of pointers of different types. While C offers flexibility, it enforces strict type compatibility to maintain type safety and prevent undefined behavior.​
Basic Pointer Compatibility
Same Type Pointers
Pointers to the same data type are naturally compatible. You can assign one to another without any issue.
int a = 30; int *p1 = &a; int *p2 = p1; // Compatible assignment
Different Type Pointers
Assigning pointers of different types without an explicit cast leads to compatibility issues, often triggering warnings or errors.
int a = 10; float *p = &a; // Incompatible pointer types
To fix this, use an explicit cast:
float *p = (float *)&a; // Explicit cast
Void Pointers
The void * type is a generic pointer type in C. It can hold the address of any data type, making it versatile for functions like memory allocation.​
int a = 10; void *vp = &a; // Compatible assignment
However, to dereference a void pointer, it must first be cast to the appropriate type:​
int *ip = (int *)vp; printf("%d", *ip);
This flexibility allows void * to serve as a universal pointer type, facilitating generic programming.​
Function Pointers
Function pointers point to functions rather than data. They are compatible only if their signatures match.​
int add(int a, int b) { return a + b; } int (*func_ptr)(int, int) = add; // Compatible assignment
If the signature doesn’t match, the compiler will flag an error or warning:
float (*wrong_ptr)(float, float) = add; // Incompatible
Const Qualifiers and Pointer Compatibility
The const qualifier affects pointer compatibility. There are three primary scenarios:​
Pointer to Const Data: The data pointed to cannot be modified.​
const int *ptr;
Const Pointer to Data: The pointer cannot point to another address, but the data can be modified.​
int *const ptr;
Const Pointer to Const Data: Neither the pointer nor the data can be modified.​
const int *const ptr;
Assigning between these types requires careful consideration to maintain const-correctness.​
Pointer Compatibility in Structures and Arrays
Structures: Pointers to different structures are incompatible, even if the structures have identical members.​
struct A { int x; }; struct B { int x; }; struct A a; struct B b; struct A *pa = &a; struct B *pb = &b; // pa = pb; // Error: incompatible pointer types
Arrays: Arrays decay to pointers to their first element. However, pointers to arrays of different types or sizes are incompatible.​
int arr[5]; float *fp = arr; // Warning or error: incompatible pointer types
To ensure compatibility, types must match exactly.​
Pointer Arithmetic and Compatibility
Pointer arithmetic is only valid within arrays of the same type. Performing arithmetic on incompatible pointer types leads to undefined behavior.​
int arr[5]; int *p = arr; p++; // Valid float *fp = (float *)p; fp++; // Potentially undefined behavior
Always ensure that pointer arithmetic is performed on compatible types to maintain program correctness.​
Best Practices for Pointer Compatibility
Avoid Implicit Conversions: Always use explicit casting when assigning between different pointer types.​
- Maintain Const-Correctness: Respect const qualifiers to prevent unintended modifications.​
- Use void * Judiciously: While versatile, void * should be used carefully to avoid type safety issues.​
- Be Cautious with Function Pointers: Ensure that function signatures match when assigning to function pointers.​
- Understand Structure and Array Compatibility: Recognize that similar-looking structures or arrays may still be incompatible.
Sanfoundry Global Education & Learning Series – 1000 C Tutorials.
- Practice Computer Science MCQs
- Practice BCA MCQs
- Watch Advanced C Programming Videos
- Apply for C Internship
- Check Computer Science Books