Consider the code below. This generates the following output:
Length: 64
Testfn: 8
Length again: 64
I am having trouble understanding why the output at the test
-function is not equal to the output at the main function. Taking into account that the output at the test
-function is equal to 8, I assume that this is because sizeof(arg)
gets the size of the pointer instead of the array itself.
This would all seem very logical to me, but why doesn't sizeof
give that same value in the main function? And how would I fix it? I have tried several things with dereferencing, but this does not seem to give any difference whatsoever.
#include <iostream>
void test(int arg[]) {
std::cout << "Testfn: " << sizeof(arg) << std::endl;
}
int main() {
int int_arr[16];
std::cout << "Length: " << sizeof int_arr << std::endl;
test(int_arr);
std::cout << "Length again: " << sizeof int_arr << std::endl;
}