in C++ by (10.3k points) edited by
0 like 0 dislike
20 views

How do I find size (length, count) of a 2D (two dimension) array in C++ programming language? Is there any predefined function like "sizeof" to determine size of array?
Also, can anyone tell me how to detect an error in the get value method for arrays while trying to get a value which is not set?


c++ 2d array size
two dimension array size in c plus plus
2d array length cpp
two dimension array count cpp

1 Answer

0 like 0 dislike
by (10.3k points) edited by

Suppose you were only allowed to use array, then you could find size/length/count of 2d (two dimension) array in C++ by several following ways:

int arr[3][2] = {{1,2}, {3,4}, {3,4}};
int size = (sizeof(arr)/sizeof(*arr));

int ary[][5] = {
    {1, 2, 3, 4, 5},
    {6, 7, 8, 9, 0}
};
int rows =  sizeof ary / sizeof ary[0]; // 2 rows  
int cols = sizeof ary[0] / sizeof(int); // 5 cols

sizeof(yourObj)/sizeOf(yourObj[0]);

int arr[6][5] = {
    {1,2,3,4,5},
    {1,2,3,4,5}
};
int rows = sizeof(arr)/sizeof(arr[0]);
int cols = sizeof(arr[0])/sizeof(arr[0][0]);

Your answer

Try to answer the question as detailed as possible.
Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.