This

int **arr = (int **)malloc(sizeof(int *) * 3);

is not a declaration or allocation of a two-dimensional array

Here a one-dimensional array with the element type int * is created. And then each element of the one-dimensional array in turn points to an allocated one dimensional array with the element type int.

This declaration of a two-dimensional array

    const int row = 3;
    const int col = 4;

    int arr[row][col] = {
            {1,2,3,4},
            {3,4,5,6},
            {5,6,7,8}
    };

is incorrect. Variable length arrays (and you declared a variable length array) may not be initialized in declaration.

You could write instead

    enum { row = 3, col = 4 };

    int arr[row][col] = {
            {1,2,3,4},
            {3,4,5,6},
            {5,6,7,8}
    };

When such an array is passed to a function it is implicitly converted to pointer to its first element of the type int ( * )[col].

You could pass it to a function that has a parameter of the type of a variable length array the following way

void    my_func( size_t row, size_t col, int arr[row][col] )
{
        printf("test2: %d", arr[0][1]);
}

Or if to place the definition of the enumeration before the function declaration

    enum { row = 3, col = 4 };

then the function could be also declared like

void    my_func( int arr[][col], size_t row )
{
        printf("test2: %d", arr[0][1]);
}

Here is a demonstrative program that shows three different approaches. The first one when an array is defined with compile-time constants for array sizes. The second one when a variable length array is created. And the third one when a one-dimensional array of pointer to one-dimensional arrays are allocated dynamically.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

enum { row = 3, col = 4 };

void output1( int a[][col], size_t row )
{
    for ( size_t i = 0; i < row; i++ )
    {
        for ( size_t j = 0; j < col; j++ )
        {
            printf( "%d ", a[i][j] );
        }
        putchar( '\n' );
    }
}

void output2( size_t row, size_t col, int a[row][col] )
{
    for ( size_t i = 0; i < row; i++ )
    {
        for ( size_t j = 0; j < col; j++ )
        {
            printf( "%d ", a[i][j] );
        }
        putchar( '\n' );
    }
}

void output3( int **a, size_t row, size_t col )
{
    for ( size_t i = 0; i < row; i++ )
    {
        for ( size_t j = 0; j < col; j++ )
        {
            printf( "%d ", a[i][j] );
        }
        putchar( '\n' );
    }
}


int     main(void)
{
        int arr1[row][col] = 
        {
                {1,2,3,4},
                {3,4,5,6},
                {5,6,7,8}
        };

        output1( arr1, row );
        putchar( '\n' );

        const size_t row = 3, col = 4;

        int arr2[row][col];

        memcpy( arr2, arr1, row * col * sizeof( int ) );

        output2( row, col, arr2 );
        putchar( '\n' );

        int **arr3 = malloc( row * sizeof( int * ) );

        for ( size_t i = 0; i < row; i++ )
        {
            arr3[i] = malloc( col * sizeof( int ) );
            memcpy( arr3[i], arr1[i], col * sizeof( int ) );
        }

        output3( arr3, row, col );
        putchar( '\n' );

        for ( size_t i = 0; i < row; i++ )
        {
            free( arr3[i] );
        }

        free( arr3 );
} 

The program output is

1 2 3 4 
3 4 5 6 
5 6 7 8 

1 2 3 4 
3 4 5 6 
5 6 7 8 

1 2 3 4 
3 4 5 6 
5 6 7 8 

Pay attention to that the function output2 can be used with the array arr1 the same way as it is used with the array arr2.

Answer from Vlad from Moscow on Stack Overflow
🌐
W3Schools
w3schools.com › c › c_arrays_multi.php
C Multidimensional Arrays (Two-dimensional and more)
Multidimensional arrays are useful ... level of structure: 2D arrays (like int scores[3][4]) are great for storing things like scores, game boards, or spreadsheets...
🌐
GeeksforGeeks
geeksforgeeks.org › c language › multidimensional-arrays-in-c
Multidimensional Arrays in C - 2D and 3D Arrays - GeeksforGeeks
4 weeks ago - For example, we can declare a two-dimensional integer array with name 'arr' with 10 rows and 20 columns as: ... We can initialize a 2D array by using a list of values enclosed inside '{ }' and separated by a comma as shown in the example below:
Discussions

Help me to understand 2D arrays in C
int *a[M] is an array of M pointers to int int (*p)[M] is a pointer to an array of M ints More on reddit.com
🌐 r/learnprogramming
2
1
April 28, 2023
How to create a 2 dimensional array in c?
You don't. Instead you make an array of arrays. C doesn't have 2d arrays. int table[5][10] is an array of 5 arrays-of-10. More on reddit.com
🌐 r/C_Programming
15
0
October 3, 2014
Passing variable size 2d-Array to C external library
You will see updates in your followed content feed. You may receive emails, depending on your communication preferences. ... Unable to complete the action because of changes made to the page. Reload the page to see its updated state. ... https://au.mathworks.com/matlabcentral/answers/1871882-passing-variable-size-2d-array... More on au.mathworks.com
🌐 au.mathworks.com
1
0
December 6, 2022
Why does this work? Pointers and 2D arrays

It's about memory layout. An array is a collection of objects in the contiguous memory location. If an array consists of subarrays (like a[2][3]), it means those subarrays are also contiguous, and you can pass from the end of the row 0 to the beginning of the row 1 seamlessly:

int a[2][3];
int *b = &a[0][2]; //pointer to the end of row 0
if(b+1 == &a[1][0])
    printf("Yes!\n");
More on reddit.com
🌐 r/C_Programming
15
2
February 8, 2024
People also ask

How to initialize a 2D array in C?
A 2D array can be initialized using braces {} while declaring it, such as: int matrix[2][2] = {{1,2},{3,4}};.
🌐
wscubetech.com
wscubetech.com › resources › c-programming › two-dimensional-array
Two-Dimensional Arrays in C Language (With Examples)
How to take input in a 2D array in C?
Input can be taken using nested loops with scanf(), allowing values to be entered for each row and column.
🌐
wscubetech.com
wscubetech.com › resources › c-programming › two-dimensional-array
Two-Dimensional Arrays in C Language (With Examples)
What is the difference between 1D and 2D arrays in C?
A 1D array stores elements linearly in a single row, accessed using one index. A 2D array stores elements in multiple rows and columns, accessed using two indices (row and column).
🌐
wscubetech.com
wscubetech.com › resources › c-programming › two-dimensional-array
Two-Dimensional Arrays in C Language (With Examples)
Top answer
1 of 5
7

This

int **arr = (int **)malloc(sizeof(int *) * 3);

is not a declaration or allocation of a two-dimensional array

Here a one-dimensional array with the element type int * is created. And then each element of the one-dimensional array in turn points to an allocated one dimensional array with the element type int.

This declaration of a two-dimensional array

    const int row = 3;
    const int col = 4;

    int arr[row][col] = {
            {1,2,3,4},
            {3,4,5,6},
            {5,6,7,8}
    };

is incorrect. Variable length arrays (and you declared a variable length array) may not be initialized in declaration.

You could write instead

    enum { row = 3, col = 4 };

    int arr[row][col] = {
            {1,2,3,4},
            {3,4,5,6},
            {5,6,7,8}
    };

When such an array is passed to a function it is implicitly converted to pointer to its first element of the type int ( * )[col].

You could pass it to a function that has a parameter of the type of a variable length array the following way

void    my_func( size_t row, size_t col, int arr[row][col] )
{
        printf("test2: %d", arr[0][1]);
}

Or if to place the definition of the enumeration before the function declaration

    enum { row = 3, col = 4 };

then the function could be also declared like

void    my_func( int arr[][col], size_t row )
{
        printf("test2: %d", arr[0][1]);
}

Here is a demonstrative program that shows three different approaches. The first one when an array is defined with compile-time constants for array sizes. The second one when a variable length array is created. And the third one when a one-dimensional array of pointer to one-dimensional arrays are allocated dynamically.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

enum { row = 3, col = 4 };

void output1( int a[][col], size_t row )
{
    for ( size_t i = 0; i < row; i++ )
    {
        for ( size_t j = 0; j < col; j++ )
        {
            printf( "%d ", a[i][j] );
        }
        putchar( '\n' );
    }
}

void output2( size_t row, size_t col, int a[row][col] )
{
    for ( size_t i = 0; i < row; i++ )
    {
        for ( size_t j = 0; j < col; j++ )
        {
            printf( "%d ", a[i][j] );
        }
        putchar( '\n' );
    }
}

void output3( int **a, size_t row, size_t col )
{
    for ( size_t i = 0; i < row; i++ )
    {
        for ( size_t j = 0; j < col; j++ )
        {
            printf( "%d ", a[i][j] );
        }
        putchar( '\n' );
    }
}


int     main(void)
{
        int arr1[row][col] = 
        {
                {1,2,3,4},
                {3,4,5,6},
                {5,6,7,8}
        };

        output1( arr1, row );
        putchar( '\n' );

        const size_t row = 3, col = 4;

        int arr2[row][col];

        memcpy( arr2, arr1, row * col * sizeof( int ) );

        output2( row, col, arr2 );
        putchar( '\n' );

        int **arr3 = malloc( row * sizeof( int * ) );

        for ( size_t i = 0; i < row; i++ )
        {
            arr3[i] = malloc( col * sizeof( int ) );
            memcpy( arr3[i], arr1[i], col * sizeof( int ) );
        }

        output3( arr3, row, col );
        putchar( '\n' );

        for ( size_t i = 0; i < row; i++ )
        {
            free( arr3[i] );
        }

        free( arr3 );
} 

The program output is

1 2 3 4 
3 4 5 6 
5 6 7 8 

1 2 3 4 
3 4 5 6 
5 6 7 8 

1 2 3 4 
3 4 5 6 
5 6 7 8 

Pay attention to that the function output2 can be used with the array arr1 the same way as it is used with the array arr2.

2 of 5
2

Suppose there is no dynamic allocation.

1   #include <stdio.h>
  1
  2 void func(int *arr, int row, int col) {
  3     int i, j;
  4
  5     for (i = 0; i < row * col; i++) {
  6         if (i && (i % col == 0))
  7             printf("\n");
  8         printf("%d ", arr[i]);
  9     }
 10
 11     printf("\n");
 12 }
 13
 14 int main(int argc, char *argv[]) {
 15     // can be this
 16     int arr1[] = {
 17         1,2,3,  // row 0
 18         4,5,6   // row 1
 19     };
 20
 21     // or this way
 22     int arr2[2][3] = {
 23         {0,1,2},  // row 0
 24         {4,5,6}   // row 1
 25     };
 26
 27     func(arr1, 2, 3);
 28     func((int*)arr2, 2, 3);
 29     return 0;
 30 }
~
🌐
Carnegie Mellon University
andrew.cmu.edu › user › gkesden › cAndUnixPrimer › 2DArrays.html
2D Arrays and Multi-Dimensional Arrays
The C Language supports multidimensional arrays. I don't know if there is a hard limit in the standard, or a practical limit adopted by the compiler -- but, in practice, you can have as many dimensions as you'd like.
🌐
TutorialsPoint
tutorialspoint.com › article › what-is-a-two-dimensional-array-in-c-language
What is a two-dimensional array in C language?
March 15, 2026 - Python TechnologiesDatabasesComputer ... All Categories ... A two-dimensional array in C is a collection of elements arranged in rows and columns, forming a matrix-like structure....
Find elsewhere
🌐
WsCube Tech
wscubetech.com › resources › c-programming › two-dimensional-array
Two-Dimensional Arrays in C Language (With Examples)
July 27, 2026 - Learn in this tutorial about Two-Dimensional Arrays in C with examples. Understand their syntax, declaration, initialization, advantages, and limitations clearly.
🌐
Sdds
intro2c.sdds.ca › two-dimensional arrays
Two-Dimensional Arrays | Introduction to C
The C language supports multi-dimensional arrays. The C compiler treats a two-dimensional array as an array of arrays. An obvious application of this data structure is an array of character strings. This chapter introduces two-dimensional arrays, describing their syntax and their organization ...
🌐
Reddit
reddit.com › r/learnprogramming › help me to understand 2d arrays in c
r/learnprogramming on Reddit: Help me to understand 2D arrays in C
April 28, 2023 -

So I've come to the conslusion that int (*arr)[M] and int *arr[M] aren't the same. int arr[M] is the array whose elements type are pointer to int. ( I think it works this way: Compiler first sees [M] and know that the array will have M elements, then sees the name ''arr'' wich later is linked to values of the array and at the end it sees the type wich is " int ".) I dont understand how it works with (*arr)[M]. Is it the same as the *arr[M] but arr is actually a pointer that points to array of ints with M elements. Please correct me if Im wrong.

🌐
Medium
medium.com › @Dev_Frank › c-multidimensional-arrays-c8e0d8bd0ce0
C-MULTIDIMENSIONAL ARRAYS. A multi-dimensional array can be termed… | by Dev Frank | Medium
February 12, 2024 - Accessing the elements of a 2D array involves specifying the row and column indices for the particular value you want to retrieve.
🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › c-language › multidimensional-arrays-c
Multidimensional Arrays (C) | Microsoft Learn
The example shows how to refer to the second individual int element of prop. Arrays are stored by row, so the last subscript varies most quickly; the expression prop[0][0][2] refers to the next (third) element of the array, and so on. ... This statement is a more complex reference to an individual element of prop.
🌐
IONOS
ionos.com › digital guide › websites › web development › 2d arrays in c
How to create and use 2D arrays in C
January 7, 2025 - Arrays make it possible to store a set of related data in C without having to create multiple variables. As a rule, arrays are one-di­men­sion­al, but they can be expanded to include any number of di­men­sions. We’ll show you how to create 2D arrays in C and how to use them ef­fec­tive­ly.
🌐
Reddit
reddit.com › r/c_programming › how to create a 2 dimensional array in c?
r/C_Programming on Reddit: How to create a 2 dimensional array in c?
October 3, 2014 -

I want to create a 2 dimensional array in C that will be filled with values from a text file. These values are integers and contain 5 rows by 10 columns. Each integer is separated by a space and each column by an end of line. I am using int[ , ] xxx = new int[5,10]; I am getting 9 errors. Ranging from C2143,C3409,C2059. What am I doing wrong?

🌐
Learningc
learningc.org › chapters › chapter09-multi-dimensional-arrays › why-2d
9.1. Why and how to use 2D arrays? — Snefru: Learning Programming with C
You can initialize a 2D array using a nested for loop. The outer loop will be responsible for looping over the row index and the inner loop can loop over the column indices for each row. For example, in the following code we initialize a 2D array using a nested for loop.
🌐
Dspmuranchi
dspmuranchi.ac.in › pdf › Blog › Two Dimensional Array in C.pdf pdf
Two Dimensional Array in C
which can be passed to any number of functions wherever required. ... The syntax to declare the 2D array is given below.
🌐
GNU
gnu.org › software › c-intro-and-ref › manual › html_node › Multidimensional-Arrays.html
Multidimensional Arrays (GNU C Language Manual)
Thus, to get the element for a particular state and year, we must subscript it first by the number that indicates the state, and second by the index for the year: ... The subarrays within the multidimensional array are allocated consecutively in memory, and within each subarray, its elements are allocated consecutively in memory.
🌐
NxtWave
ccbp.in › blog › articles › two-dimensional-array-in-c
Two-Dimensional Arrays in C: Applications, Initialization & Uses
September 10, 2025 - While passing the array, the first dimension can be left unspecified, but the second dimension must be fixed as [3]. ... Basically, a 2D array in C is an array of arrays. It enables you to store data in rows and columns that resemble tables.
🌐
Diveintosystems
diveintosystems.org › book › C2-C_depth › arrays.html
and two-dimensional arrays in C
Both the matrix and the bigger arrays can be passed as arguments to the init_matrix function because they have the same column dimension as the parameter definition. Statically allocated 2D arrays are arranged in memory in row-major order, meaning that all of row 0’s elements come first, followed by all of row 1’s elements, and so on.
🌐
Programiz
programiz.com › c-programming › c-multi-dimensional-arrays
C Multidimensional Arrays (2d and 3d Array)
// C program to find the sum of two matrices of order 2*2 #include <stdio.h> int main() { float a[2][2], b[2][2], result[2][2]; // Taking input using nested for loop printf("Enter elements of 1st matrix\n"); for (int i = 0; i < 2; ++i) for (int j = 0; j < 2; ++j) { printf("Enter a%d%d: ", i + 1, j + 1); scanf("%f", &a[i][j]); } // Taking input using nested for loop printf("Enter elements of 2nd matrix\n"); for (int i = 0; i < 2; ++i) for (int j = 0; j < 2; ++j) { printf("Enter b%d%d: ", i + 1, j + 1); scanf("%f", &b[i][j]); } // adding corresponding elements of two arrays for (int i = 0; i < 2; ++i) for (int j = 0; j < 2; ++j) { result[i][j] = a[i][j] + b[i][j]; } // Displaying the sum printf("\nSum Of Matrix:"); for (int i = 0; i < 2; ++i) for (int j = 0; j < 2; ++j) { printf("%.1f\t", result[i][j]); if (j == 1) printf("\n"); } return 0; }
🌐
MathWorks
au.mathworks.com › matlabcentral › answers › 1871882-passing-variable-size-2d-array-to-c-external-library
Passing variable size 2d-Array to C external library - MATLAB Answers - MATLAB Central
December 6, 2022 - As in MATLAB if you have above mentioned matrix A then internally MATLAB is handling that matrix as 1D array as shown above. If you get double pointer Aptr from the below command: ... You can just pass that Aptr to C library function that accepts double* data type variable. And in C code you can consider the 2D Matrix A from MATLAB as 1D array in C with elements arranged column wise as explained above.
🌐
Facebook
facebook.com › groups › cs50 › posts › 3094210020726022
Can C support 2D arrays with different data types?
Popular groups · Find communities for you · Over 1 billion people across the globe are using Facebook Groups to explore their favorite topics · Log in · Categories · Science & tech · Travel · Animals · Sports & fitness · Entertainment