1

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);
}
5
  • 5
    You don't - not possible Commented May 12, 2011 at 18:17
  • 1
    @JT (a very annoying tag, BTW) Not generally possible would be more accurate :-) Commented May 12, 2011 at 18:20
  • I wonder whats the use of int (*a)[5] if its not workable. Commented May 12, 2011 at 18:24
  • It is just history from the C language. You can use arrays as parameter types, but it decays to pointers anyway. If you use C++ you should use a std::vector which knows its own size. Commented May 12, 2011 at 18:27
  • You may want to read our array FAQ. Commented May 12, 2011 at 18:58

7 Answers 7

3

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 it

void Func(int (*a)[5])
{
   for (int i = 0; i < 5; i++)
      cout << (*a)[i];
}

Secondly, 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 sizeof trick

void Func(int (*a)[5])
{
   for (int i = 0; i < sizeof *a / sizeof **a; i++)
      cout << (*a)[i];
}

The sizeof *a / sizeof **a expression is guaranteed to evaluate to 5 in this case.

Sign up to request clarification or add additional context in comments.

Comments

3

Using a function template would be better if you major emphasis is on passing the size of the array.

template <typename T, size_t N>
void Func(T (&a)[N])
{  
    for (int i = 0; i < N; i++)
      cout << a[i]; 
}

Comments

1

It doesn't work because you didn't de-reference it enough. You have a pointer to an array- the pointer requires de-referencing, and the array needs indexing.

void Func(int (*a)[5])
{
   for (int i = 0; i < 5; i++)
      std::cout << (*a)[i];
}

int main()
{
   int a[] = {1, 2, 3, 4, 5};
   Func(&a);
}

Comments

0

When you pass an array into a function, it degrades to a pointer and all size information is lost.

Comments

0

You can instead use a STL container, like vector, to make things easier.

#include <iostream>
#include <vector>
using namespace std;

void Func(vector <int> a){
   for (int i = 0; i < a.size(); i++)
   cout << a[i];
}

int main (){
       int nums[] = {1, 2, 3, 4, 5};
       vector<int> a (nums, nums + sizeof(nums) / sizeof(int) );
       Func(a);
}

Comments

0

How do i get the size of array

You already know the size at compile-time, it's 5. You just forgot to dereference the pointer:

cout << (*a)[i];

2 Comments

What about int size = sizeof(*a)/sizeof(int);?
@user: You already told the compiler that you point to an array of 5 integers. What else do you hope to find out about the array? Yes, sizeof(*a)/sizeof(int) will yield 5, but again, you already know that.
-1
void Func(int * a)
{
   for (int i = 0; i < 5; i++)
      cout << *(a+i);
}

int main()
{
   int a[] = {1, 2, 3, 4, 5};
   Func(a);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.