Compilation (gcc error)Type & Declaration5 min read

Fix “subscripted value is neither array nor pointer” in C

In C, `a[i]` is syntactic sugar for `*(a + i)`, which is only legal when one operand is a pointer to a complete object type (or an array that decays to one) and the other is an integer.

Quick Fix Summary

Declare array brackets [] or pointer * on variables you index, cast void * to a concrete pointer type before indexing, and remove extra [i] layers on 1D variables.

Why This Error Happens

Scalar variables accidentally indexed

When int count = 5; is indexed as count[0], the compiler catches a scalar masquerading as an array.

Dereferencing a void * pointer

void * has no element size, so standard ISO C rejects ptr[i] on void *. Assign to unsigned char * or int * first.

Top Root Causes & Specific Code Fixes

Indexing a scalar variable or applying one too many [][j]

Passing 1D matrix rows via matrix[i][j] when matrix was declared as int * or int fails.

Broken Pattern
int value = 10;
printf("%d", value[0]);
Corrected Pattern
int values[1] = {10};
printf("%d\n", values[0]);
Illustrative diagnostic (not executed):error: subscripted value is neither array nor pointer nor vector

Indexing directly into malloc return assigned as void *

Standard C forbids pointer arithmetic and [] indexing on untyped void *.

Broken Pattern
void *buf = malloc(16);
buf[0] = 1;
Corrected Pattern
unsigned char *buf = malloc(16);
if (buf) { buf[0] = 1; free(buf); }

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>
int main(void) {
    int score = 95;
    printf("%d\n", score[0]);
    return 0;
}
Open broken program in compiler
Read the corrected program
#include <stdio.h>
int main(void) {
    int scores[1] = {95};
    printf("%d\n", scores[0]);
    return 0;
}
Open corrected program in compiler

Expected output from the corrected program

Reference output, not a live execution result.

95

Compiler & Sanitizer Diagnostic Matrix

Compiler / Runtime SymptomRoot CauseDiagnostic Flag / CheckCorrected Pattern
subscripted value is neither array nor pointerApplying `[i]` to scalar `int` or `void *`-Wpedantic -WallIndex typed arrays/pointers (`unsigned char *`, `int *`)
extra dimension index `[i][j]` on `int *`Mismatch between 1D pointer and 2D indexing-WallDeclare inner width `int (*m)[COLS]` or flatten `m[i * COLS + j]`

▶ Load failing reproducer in Online C Compiler

Debugging Techniques and Commands

Strict ISO C Checks
gcc -std=c17 -Wpedantic -Wall -Wextra main.c -o app

Flags nonstandard GNU void* arithmetic extensions immediately.

Type Inspection
gcc -std=c17 -Wall -c main.c

Checks array vs pointer parameter types at function boundaries.

Frequently Asked Questions

Why does int f(int arr[][]) also fail?

C requires all array dimensions after the first (`int arr[][4]`) to be specified so stride math is known.

Wait — does GCC allow void* arithmetic without -Wpedantic?

GCC permits void* +/- as a GNU extension treating sizeof(void)==1, whereas `-Wpedantic` and Clang strict mode flag `void*` arithmetic and `[]` indexing.

Does a[i] always mean *(a + i)?

Yes. Standard C defines array subscripting directly in terms of pointer addition and dereference.