0

if we can pass array size in square brackets ?when function is declared or array is pass as an argument

4th Aug 2016, 2:32 PM
subha21
7 Réponses
+ 3
You can pass an array in like this: void func(int arr[]) { //code } int main() { int arr[5]; func(arr); } (pass by reference if you need to) or if you only want to pass the array's size: void func(int b) { //code } int main() { const int a = 5; int arr[5]; func(x); }
4th Aug 2016, 3:01 PM
Cohen Creber
Cohen Creber - avatar
+ 2
no u can't
4th Aug 2016, 4:17 PM
hardik aseri
hardik aseri - avatar
+ 2
u can only write the array name as a parameter and it's size as another parameter inside void functions. But don't forget to separate the two parameters with a comma
6th Aug 2016, 10:48 AM
Baba Yunus Abdul Yekin
Baba Yunus Abdul Yekin - avatar
+ 1
You can use sizeof operator. sizeof(arr) should return the same number as you see in the square bracket.
5th Aug 2016, 4:03 AM
Nick D.
Nick D. - avatar
0
that's not my question i wanna know if i can pass array size in( void func )inside the square brackets
4th Aug 2016, 3:07 PM
subha21
0
ok thanks
5th Aug 2016, 3:42 AM
subha21
0
That's not possible. Say you have a function func() that takes an array as an argument, in that case you will only pass the name of the array, without the square brackets, to that function: int arr[3]={2,7,4}; func(arr); if you were to pass the name WITH the square brackets, you get an error, and if you were to pass arr[2] for example, it would be resolved to the CONTENT of arr[2]. Finally, for an array with the length x, the highest index would be x-1, so if arr had a length of 3, the indices woul be 0 1 2 , thus, arr[3] would point outside the range of the array.
10th Sep 2016, 12:13 AM
Andres Mar