Home > Software engineering >  2D array size in bytes
2D array size in bytes

Time:10-11

I would like to know, how to calculate 2D array's size in bytes.

Array: int data[][5] = {{1, 2, 3}, {0}, {4, 3, 2}};

And how should I calculate the size of this array in bytes.

Programming language is C.

CodePudding user response:

You can use the sizeof operator. The expression sizeof data will give you the size of the array, in bytes.

Here is a short demonstration program:

#include <stdio.h>

int main( void )
{
    int data[][5] = {{1, 2, 3}, {0}, {4, 3, 2}};

    printf( "The size of the array is: %zu\n", sizeof data );
}

In my test environment, this program has the following output:

The size of the array is: 60

The size of the array may be different on different platforms, but on most modern platforms, sizeof(int) is 4, so the array's total size is 4 * 3 * 5, which is 60.

  • Related