Passing Array to function

Aug 20, 2018 at 10:39am
I get 1 as output. Do you what causes that problem?
By the way the function is located in a header file.
I tried everything I could. Really.

main.cpp:
1
2
3
4
5
6
7
8
9
  int main()
{
	int myarr[3] = { 3,2,1 };
	int input;

	cout << get_arraysize(myarr, &input) << endl;
	cin.get();
    return 0;
}


function in test.h:
1
2
3
4
5
6
        #define ARRAY_SIZE(array) (sizeof((array))/sizeof((array[0])))

	int get_arraysize(int arr[], int* output) {
		*output = ARRAY_SIZE(arr);
		return *output;
	}
Last edited on Aug 20, 2018 at 10:40am
Aug 20, 2018 at 10:52am
The thing that gets passed to your function is not actually an array, but a pointer. Since a pointer is a single integer, the result of the division is 1.

By the way, what's the point of your ARRAY_SIZE macro? What does that achieve?
Aug 20, 2018 at 10:58am
That could happen because inside the function it isn't known which size the array has. The size on an array is only known within the translation unit where it is defined. Consider: If an array will be passed into a function, this function will never know how large the array is. Internally, passing an array will be handled as a pointer.

Btw: Why do you define your function inside a header? The definition should be seperated to an ordinary source file.
Last edited on Aug 20, 2018 at 11:04am
Aug 20, 2018 at 11:06am
I made a macro to get the size of the array.
Is there a way to let the function know the arrays size?
Aug 20, 2018 at 11:07am
> I tried everything I could. Really.

Try passing the array by reference; that would avoid the array to pointer decay.

For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <iterator> // for std::size (C++17)

template < typename T, std::size_t N >
constexpr std::size_t array_size( T(&)[N] ) { return N ; }

int main()
{
    const int arr1[] { 0, 1, 2, 3, 4 } ;
    std::cout << array_size(arr1) << ' ' << std::size(arr1) << '\n' ; // 5 5

    const double arr2[] { 0.12, 3.45, 6.78, 9.01, 2.34, 5.67, 8.90 } ;
    std::cout << array_size(arr2) << ' ' << std::size(arr2) << '\n' ; // 7 7
}

http://coliru.stacked-crooked.com/a/48676188b8303d07
Aug 20, 2018 at 11:09am
I made a macro to get the size of the array.

Yes, I can see what the macro is doing. It's not exactly rocket science.

I was asking why you bothered to put it in a macro. What do you achieve by doing that?

Is there a way to let the function know the arrays size?

The same way as you'd let a function know any other information: by passing it in as an argument.

That's the typical pattern for passing C-style arrays into a function: pass the pointer, and pass the size as a separate argument.

And, since no-one else has said it yet: I'd advise you to move away from using C-style arrays, and learn how to use STL containers, and in particular, std::vector.
Last edited on Aug 20, 2018 at 11:11am
Aug 20, 2018 at 11:13am
just making it look easier for me

What should i achieve else?
Aug 20, 2018 at 11:14am
Thanks JLBorges
I just tried that and it worked fine
Aug 20, 2018 at 11:19am
MikeyBoy Thanks for the tip I'll take a look at that
Topic archived. No new replies allowed.