Search code examples
carraysmultidimensional-arraytypedef

Array type 'some_typedef_name' is not assignable


I have a two-dimensional array:

(in a header file)
#define N 11
typedef char adjmat[N][N];

When I declare the array in the main function

adjmat mat[N][N];

I can create the array with values

adjmat mat[N][N] = {{1, 1, 1},{0, 0, 0}};

and read the values from the array

char example = mat[0][0];

but when I try to assing a value to a cell in the array

mat[i][j] = getchar() - '0';

I get an error:

Array type 'adjmat' (aka 'char[11][11]') is not assignable

how can I assign a value to the array's cells?


Solution

  • You have already typedef-ed the two dimensional array.

    #include <stdio.h>
    
    #define N 11
    typedef char adjmat[N][N];
    
    
    void foo()
    {
        adjmat mat = {{1,2,3,}, {4,5,6,}, };
    
        for(int row = 0; row < N; row ++)
        {
            for(int col = 0; col < N; col++)
            {
                printf("%03d ", mat[row][col]);
            }
        printf("\n");
        }
    }
    
    int main()
    {
        foo();
    }
    

    if you declare

    adjmat mat[N][N];
    

    you declare the two-dimensional array of the two-dimensional arrays