Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок0
if we can pass array size in square brackets ?when function is declared or array is pass as an argument
7 ответов
+ 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);
}
+ 2
no u can't
+ 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
+ 1
You can use sizeof operator. sizeof(arr) should return the same number as you see in the square bracket.
0
that's not my question i wanna know if i can pass array size in( void func )inside the square brackets
0
ok thanks
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.