Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to find 2d array size in c++

Tags:

c++

arrays

size

How do I find the size of a 2D array in C++? Is there any predefined function like sizeof to determine the size of the array?

Also, can anyone tell me how to detect an error in the getvalue method for arrays while trying to get a value which is not set?

like image 333
user1322915 Avatar asked Apr 23 '12 02:04

user1322915


People also ask

How do you find the size of a 2D array?

We use arrayname. length to determine the number of rows in a 2D array because the length of a 2D array is equal to the number of rows it has. The number of columns may vary row to row, which is why the number of rows is used as the length of the 2D array.

How do you find the size of an array in C?

We can find the size of an array using the sizeof() operator as shown: // Finds size of arr[] and stores in 'size' int size = sizeof(arr)/sizeof(arr[0]);

What is the maximum size of 2D array in C?

If the array is declared using automatic storage duration, then the size limit is remarkably small, at around 1Mb. If you're using dynamic storage duration (using new and new[] ), then then limit is much higher.

What is the maximum size of 2D array?

C++, Using 2D array (max size 100*26) - LeetCode Discuss.


2 Answers

Suppose you were only allowed to use array then you could find the size of 2-d array by the following way.

  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 
like image 62
SridharKritha Avatar answered Sep 26 '22 10:09

SridharKritha


#include <bits/stdc++.h> using namespace std;   int main(int argc, char const *argv[]) {     int arr[6][5] = {         {1,2,3,4,5},         {1,2,3,4,5},         {1,2,3,4,5},         {1,2,3,4,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]);     cout<<rows<<" "<<cols<<endl;     return 0; } 

Output: 6 5

like image 43
bitbyter Avatar answered Sep 24 '22 10:09

bitbyter