Fix “incompatible pointer types” in C
Starting in GCC 14 and modern Clang, `-Wincompatible-pointer-types` is promoted to a hard compilation error by default because reading or writing through a mismatched pointer type violates strict aliasing and pointer stride arithmetic.
Quick Fix Summary
Match parameter types to the caller: accept size_t cols, int grid[][cols] (or a flat int *) instead of int ** for contiguous 2D arrays, preserve const qualifiers on read-only string parameters, and give qsort comparators exact const void * parameters.
Why This Error Happens
Why int matrix[3][4] is incompatible with int **
A contiguous 2D array stores 12 integers back-to-back in row-major order and decays to a row pointer int (*)[4], whereas int ** expects an array of pointer addresses in memory.
Why void ** is not a generic pointer-to-pointer
Although void * converts to/from any object pointer in C, void ** does NOT convert implicitly from int ** because different pointer representations are permitted by the C standard.
Top Root Causes & Specific Code Fixes
Passing a 2D array int m[2][3] to a parameter declared int **
Row arrays decay to pointer-to-row `int (*)[3]`, not pointer-to-pointer.
void show(int **m) { printf("%d\n", m[0][1]); }
int grid[2][3] = {{1, 2, 3}, {4, 5, 6}};
show(grid);void show(size_t cols, const int m[][cols]) { printf("%d\n", m[0][1]); }
int grid[2][3] = {{1, 2, 3}, {4, 5, 6}};
show(3, grid);Passing &ptr (int **) to a parameter expecting void **
Only single-level `void *` acts as a generic object pointer in ISO C.
void reset(void **slot) { *slot = NULL; }
int *p = NULL;
reset(&p);int *p = NULL;
p = NULL;Complete Broken and Corrected Programs
Compare the source before running it. The broken program is intentionally incorrect; its behavior can depend on the compiler and platform.
Read the broken program
#include <stdio.h>
void print_cell(int **m) {
printf("%d\n", m[1][2]);
}
int main(void) {
int grid[2][3] = {{1, 2, 3}, {4, 5, 6}};
print_cell(grid);
return 0;
}Open broken program in compilerRead the corrected program
#include <stdio.h>
void print_cell(size_t cols, const int m[][cols]) {
printf("%d\n", m[1][2]);
}
int main(void) {
int grid[2][3] = {{1, 2, 3}, {4, 5, 6}};
print_cell(3, grid);
return 0;
}Open corrected program in compilerExpected output from the corrected program
Reference output, not a live execution result.
6Debugging Techniques and Commands
gcc -std=c17 -Wall -Wextra -Werror=incompatible-pointer-types main.c -o app
Prints both expected and actual pointer declarations side by side.
gcc -std=c17 -Wcast-qual main.c -o app
Warns whenever a cast strips away const or volatile qualifiers.
Frequently Asked Questions
Should I silence incompatible pointer types with a (void *) or (int **) cast?
Almost never. Adding a cast hides the compiler error while leaving broken pointer stride or strict-aliasing undefined behavior at runtime.
Why did old code compile on GCC 11 but fail on GCC 14?
GCC 14 upgraded `-Wincompatible-pointer-types` from a warning to a default error to align with C99/C17 type safety.
How do I write a comparator for qsort without pointer warnings?
Declare `int cmp(const void *a, const void *b)` and read `const int *ia = a; const int *ib = b;` inside the body.