Deep Analysis of Arrays and Pointers in C: Resolving the "Subscripted Value Is Neither Array Nor Pointer" Error

Nov 23, 2025 · Programming · 19 views · 7.8

Keywords: C Language | Array Pointers | Multidimensional Arrays | Function Parameters | Struct Encapsulation

Abstract: This article provides an in-depth analysis of the common C language error "subscripted value is neither array nor pointer nor vector", exploring the relationship between arrays and pointers, array parameter passing mechanisms, and proper usage of multidimensional arrays. By comparing erroneous code with corrected solutions, it explains the type conversion process of arrays in function parameters and offers best practices using struct encapsulation for fixed-size arrays to help developers avoid common pitfalls.

Problem Background and Error Analysis

In C programming, developers frequently encounter the "subscripted value is neither array nor pointer nor vector" compilation error. This error typically occurs when attempting to use the subscript operator on a value that is neither an array nor a pointer. From the provided code example, the error appears in the following statement:

D[i][n] = arr[n][M - i + 1];

The fundamental issue here is the mismatch between the function parameter arr's type declaration and the actual array type.

Deep Relationship Between Arrays and Pointers

C language allows the use of subscript operator [] on both arrays and pointers. When this operator is applied to a pointer, the resulting type is the type pointed to by the pointer. For example, applying [] to int* yields an int type.

In the original code, the function parameter is declared as int *arr, indicating that arr is a pointer to an integer. When arr[n] is used, the result is an int value, not an array or pointer. Therefore, applying a second subscript operator [M - i + 1] triggers a compilation error because integer types do not support subscript operations.

Multidimensional Array Parameter Passing Mechanism

When passing multidimensional arrays to functions, C performs specific type conversions. For a two-dimensional array declared as int S[4][4], in the function call rotateArr(S), the expression S has type "4-element array of 4-element arrays". Since this is not the operand of sizeof or the address-of operator, it converts to a pointer to a 4-element array, specifically int (*)[4] type.

Therefore, the correct function parameter types should be one of the following:

Problem with Returning Local Arrays and Solutions

The original code attempts to return local array D, which is not permitted in C because local arrays are destroyed after function return, making any returned pointer point to invalid memory. While dynamic memory allocation could solve this, a more elegant approach is to use struct encapsulation for fixed-size arrays.

Here is the improved code implementation:

typedef struct {
    int arr[4][4];
} FourByFour;

FourByFour rotate(FourByFour m) {
    FourByFour D;
    for(int i = 0; i < 4; i++) {
        for(int n = 0; n < 4; n++) {
            D.arr[i][n] = m.arr[n][3 - i];
        }
    }
    return D;
}

This method leverages C's struct value copy feature to safely return the entire array content.

Complete Example and Verification

Here is a complete usage example:

int main(void) {
    FourByFour S = {.arr = {
        { 1, 4, 10, 3 },
        { 0, 6, 3, 8 },
        { 7, 10, 8, 5 },
        { 9, 5, 11, 2}
    }};
    
    FourByFour r = rotate(S);
    
    for(int i = 0; i < 4; i++) {
        for(int n = 0; n < 4; n++) {
            printf("%d ", r.arr[i][n]);
        }
        printf("\n");
    }
    return 0;
}

This program outputs the rotated matrix:

3 8 5 2 
10 3 8 11 
4 6 10 5 
1 0 7 9

Summary and Best Practices

Through this analysis, we can draw the following important conclusions:

  1. Understanding array-to-pointer decay rules is crucial
  2. Multidimensional arrays as function parameters require correct dimension declarations
  3. Avoid returning local arrays; struct encapsulation is a safer alternative
  4. Clearly distinguishing between pointer types and multidimensional array types prevents many common errors

These principles apply not only to the current array rotation problem but also represent general best practices for handling complex data structures in C.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.