I have 2 arrays, 1 is a 1D array, and the second is a 2D array.
I pass both of them into a function. If I try to use the sizeof
operator in the function, and in the main
itself. But, it gives me 2 different outputs.
This is my program::
#include <iostream>
using namespace std;
void test(int *a,int b[][10]){
cout<<"Inside Test"<<endl;
cout<<sizeof(a)<<endl;
cout<<sizeof(b)<<endl;
}
int main() {
// your code goes here
int a[10];
int b[10][10];
cout<<sizeof(a)<<endl;
cout<<sizeof(b)<<endl;
test(a,b);
return 0;
}
This is the output::
40
400
Inside Test
4
4
The output in the function test
is the size of the pointer according to me. (Please correct me if I am wrong) One possible reason which I believe is that the function call copies the address of my array to some other location for the function to access, because of which I get 4 as the output.
But, is there a way that I can use the sizeof operator in the function in the test
and get the correct size printed. Or is there any other way to get the correct size. And further is the reason that I gave above for the output as 4
is correct or not?
Thanks for any help in advance.. :D