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
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ multidimensional-arrays-in-c
Multidimensional Arrays in C - 2D and 3D Arrays - GeeksforGeeks
2 weeks ago - It can be viewed as multiple one-dimensional arrays arranged in a table. If a 2-D array has m rows and n columns, the row indices range from 0 to mโˆ’1 and the column indices range from 0 to nโˆ’1. In C, array indexing starts from 0, so both ...
๐ŸŒ
W3Schools
w3schools.com โ€บ c โ€บ c_arrays_multi.php
C Multidimensional Arrays (Two-dimensional and more)
To create a 2D array of integers, take a look at the following example: ... The first dimension represents the number of rows [2], while the second dimension represents the number of columns [3]. The values are placed in row-order, and can be visualized like this: To access an element of a two-dimensional array, you must specify the index number of both the row and column.
Discussions

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
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 slice 2d array in C# 8.0?

I want to slice 2d arrays like you can using NumSharp:

https://medium.com/scisharp/slicing-in-numsharp-e56c46826630

It's impossible in C# 8.0?

More on reddit.com
๐ŸŒ r/csharp
9
10
December 1, 2019
Qsort a 2d array of strings.
'ar' is an array with 4 elements, and C arranges these in memory sequentially, which is what qsort is expecting. ptr is only long enough to hold two elements, and you are telling qsort that it has 4. It doesn't understand this whole pointers-to-pointers-to-pointers structure you are attempting to use. Try treating ptr as a single dimensional array, and doing the subscript lookup math yourself. More on reddit.com
๐ŸŒ r/C_Programming
5
2
November 24, 2018
People also ask

Can we return a two-dimensional array from a function in C?
Directly returning a local 2D array isn't possible in C. Instead, you can dynamically allocate it and return its pointer or wrap it inside a struct.
๐ŸŒ
wscubetech.com
wscubetech.com โ€บ resources โ€บ c-programming โ€บ two-dimensional-array
Two-Dimensional Arrays in C Language (With Examples)
What are practical uses of two-dimensional arrays in C?
Common practical uses include representing matrices, handling images, game boards, grids, tables, and spreadsheets.
๐ŸŒ
wscubetech.com
wscubetech.com โ€บ resources โ€บ c-programming โ€บ two-dimensional-array
Two-Dimensional Arrays in C Language (With Examples)
What is a 2D array in C?
A 2D array in C is a collection of elements stored in rows and columns, allowing data to be organized in a table-like structure.
๐ŸŒ
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 }
~
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ c multidimensional arrays (two dimensional array in c)
Two Dimensional Array in C | Multidimensional Array in C - Scaler Topics
May 1, 2024 - This sets the horizontal length of the array. Choose from our industry-leading programs designed for career success ... In C programming, a two-dimensional (2D) array is a powerful tool that mimics a table, made up of rows and columns, to store data of a single type.
๐ŸŒ
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 - A two-dimensional array in C is a collection of elements arranged in rows and columns, forming a matrix-like structure. It is essentially an array of arrays, where each element is identified by two indices: row index and column index.
๐ŸŒ
BeginnersBook
beginnersbook.com โ€บ 2014 โ€บ 01 โ€บ 2d-arrays-in-c-example
Two dimensional (2D) arrays in C programming with example
July 25, 2022 - The array that we have in the example below is having the dimensions 5 and 4. These dimensions are known as subscripts. So this array has first subscript value as 5 and second subscript value as 4. So the array abc[5][4] can have 5*4 = 20 elements. To store the elements entered by user we are using two for loops, one of them is a nested loop.
Find elsewhere
๐ŸŒ
NxtWave
ccbp.in โ€บ blog โ€บ articles โ€บ two-dimensional-array-in-c
Two-Dimensional Arrays in C: Applications, Initialization & Uses
... This creates a matrix with three columns and four rows. Every element in the matrix can be accessed by providing its matching row and column indices. In C, two-dimensional arrays are organized in a way called row-major order.
๐ŸŒ
WsCube Tech
wscubetech.com โ€บ resources โ€บ c-programming โ€บ two-dimensional-array
Two-Dimensional Arrays in C Language (With Examples)
March 17, 2026 - Learn in this tutorial about Two-Dimensional Arrays in C with examples. Understand their syntax, declaration, initialization, advantages, and limitations clearly.
๐ŸŒ
Tutorial Gateway
tutorialgateway.org โ€บ two-dimensional-array-in-c
Two Dimensional Array in C
February 10, 2026 - 2D or Two Dimensional Array in C is a matrix and it stores the data in rows and columns. To access them use for loop, row, column, and index.
๐ŸŒ
IONOS
ionos.com โ€บ digital guide โ€บ websites โ€บ web development โ€บ 2d arrays in c
How to create and use 2D arrays in C - IONOS
January 7, 2025 - 2D arrays can be created and filled with the following syntax: ints_two_dimensions[10][10]; ints_two_dimensions [0][1] = 0; ints_two_dimensions [2][1] = 2; ints_two_dimensions [9][4] = 36; // etc.c ยท
๐ŸŒ
Vaia
vaia.com โ€บ 2d array in c
2d Array in C: Definition & Examples | Vaia
December 12, 2024 - 2d Array in C Definition: A 2D array in C is a grid-like structure, essentially arrays of arrays, useful for organizing data in rows and columns. It facilitates operations on two-dimensional data structures such as matrices.
๐ŸŒ
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?

๐ŸŒ
Dspmuranchi
dspmuranchi.ac.in โ€บ pdf โ€บ Blog โ€บ Two Dimensional Array in C.pdf pdf
Two Dimensional Array in C
The syntax to declare the 2D array is given below. ... Consider the following example. ... Here, 4 is the number of rows, and 3 is the number of columns.
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ two-dimensional-arrays-in-c
Two-dimensional arrays in C
A two-dimensional array in C can be thought of as a matrix with rows and columns.
๐ŸŒ
Upgrad
upgrad.com โ€บ home โ€บ tutorials โ€บ software & tech โ€บ two dimensional array in c
Two dimensional array in C : Concept & Usage
March 16, 2026 - While one-dimensional arrays allow you to store a linear list of values, a two dimensional array in C gives you the ability to organize data in rows and columns, just like a spreadsheet or matrix.
๐ŸŒ
Carnegie Mellon University
andrew.cmu.edu โ€บ user โ€บ gkesden โ€บ cAndUnixPrimer โ€บ 2DArrays.html
2D Arrays and Multi-Dimensional Arrays
So, what C really needs to do to keep things fast and simple is to project a multi-dimensional array into a one-dimensionaly memory. And, this is exactly what it does. We'll take a look at the case of a two-dimensional array.
๐ŸŒ
Sdds
intro2c.sdds.ca โ€บ two-dimensional arrays
Two-Dimensional Arrays | Introduction to C - Table of contents
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 ...
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ two-dimensional-array-in-c-plus-plus
2D Arrays in C++: Declare, Initialize & Operations | DigitalOcean
1 month ago - A two-dimensional array in C++ provides a powerful way to organize data into a grid-like table of rows and columns. This article will walk you through the fundamentals of declaring and initializing 2D arrays, including operations like handling user input and performing matrix addition.
๐ŸŒ
CodinGeek
codingeek.com โ€บ home โ€บ 2d arrays in c language - how to declare, initialize and access elements
2D Arrays in C - How to declare, initialize and access
April 11, 2021 - A 2D array is like a matrix and has a row and a column of elements ( Although in memory these are stored in contiguous memory locations). A 1-D array, as we saw in the previous tutorial, is a linear list of data and needed only one index to ...
๐ŸŒ
PW Skills
pwskills.com โ€บ blog โ€บ cpp โ€บ two-dimensional-array-in-c
Two Dimensional Array in C (With Examples): Useful Guide (2025)
A two dimensional array in C is an array of arrays. To put it simply, it is a sort of table with your data arranged in rows and columns. In contrast to one-dimensional arrays that only hold a single row of data, the two dimensional arrangement ...