Compilation (gcc error)Type & Declaration6 min read

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.

Broken Pattern
void show(int **m) { printf("%d\n", m[0][1]); }
int grid[2][3] = {{1, 2, 3}, {4, 5, 6}};
show(grid);
Corrected Pattern
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);
Illustrative diagnostic (not executed):error: passing argument 1 of show from incompatible pointer type

Passing &ptr (int **) to a parameter expecting void **

Only single-level `void *` acts as a generic object pointer in ISO C.

Broken Pattern
void reset(void **slot) { *slot = NULL; }
int *p = NULL;
reset(&p);
Corrected Pattern
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 compiler
Read 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 compiler

Expected output from the corrected program

Reference output, not a live execution result.

6

Debugging Techniques and Commands

Strict Pointer Checks
gcc -std=c17 -Wall -Wextra -Werror=incompatible-pointer-types main.c -o app

Prints both expected and actual pointer declarations side by side.

Const Qualification Check
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.