How do i get the size of array in void Func(int (*a)[5]) for loop condition and print elements?
void Func(int (*a)[5])
{
for (int i = 0; i < 5; i++)
cout << a[i]; // doesn't work
}
int main()
{
int a[] = {1, 2, 3, 4, 5};
Func(&a);
}
Firstly, the way you access the array is wrong. Inside your function you have a pointer to an array. The pointer has to be dereferenced first, before you use the
[]operator on itSecondly, i don’t understand why you need to “get” the size of the array, when the size is fixed at compile time, i.e. it is always 5. But in any case you can use the well-known
sizeoftrickThe
sizeof *a / sizeof **aexpression is guaranteed to evaluate to 5 in this case.