+ 2
How to find the length of this array?
int a[1][2][3]={{1},{1,2},{1,2,3}}; How can i find each part of this array by sizeof()? I mean the first part length is one..the third one is thre...
7 ответов
+ 3
The array you've declared is invalid because
1. You have declared a 3-dimensional array, but initialized a 2-dimensional array
2. The outermost array is supposed to have 1 inner array, but in the initialization you have given 3
You might want to see how you declare multi-dimensional arrays
https://www.sololearn.com/learn/C/2932/
+ 2
Ali_combination just like you find the size of an array. By doing
sizeof(array) / sizeof(data type of elements of array)
In this case, if you want to find the size of the first inner array (which will be the same for all inner arrays), just do the same
sizeof(a[0]) / sizeof(a[0][0])
Working example
https://code.sololearn.com/cnG7vZRZguS8/?ref=app
+ 2
Ali_combination I forgot to give an explanation for my answer. The size of an array in bytes is basically-
number of elements of array * size of each element of array in bytes
For example,
int arr[] = {1, 2, 3, 4};
Calling sizeof on arr will give
(number of elements in arr = 4) * (size of int = 4) = 16
That is why, if you know the size of each element of the array, and the size of the array, both of which you can get from sizeof, you can get the length
size of array / size of each element = number of elements in array
+ 1
XXX Thanks! Now could you plz let me know how to find length of each part??
0
I think this works :
char a[2][3]={{2,2,3},{2,3,1}}
int b=sizeof(a[0]);
Cout<<b;
//output is 3.
0
XXX worked..thanks for your precious help.
0
XXX True ! Thanks !👍👍