Passing Array to a function doubt?

Jun 15, 2013 at 9:15am
Hi all.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;

void getSizeOfArray(int newArray[]); // creating a prototype

int main(){
    int myArray[3];
    //cout << sizeof(myArray) << endl;
    getSizeOfArray(myArray);
}

void getSizeOfArray(int newArray[]){
    cout << sizeof(newArray);
}


As far as I can reckon it, the above code should be displaying 12( uncomment the 8th line and it will display 12 ).
But it is displaying 4.

Why is this happening? please help.
Jun 15, 2013 at 9:37am
When an array is passed as a parameter to a function, it is treated identically to a pointer; this is referred to as array decaying. So your program is equivalent to this one:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;

void getSizeOfArray(int* newArray); // creating a prototype

int main(){
    int myArray[3];
    //cout << sizeof(myArray) << endl;
    getSizeOfArray(myArray);
}

void getSizeOfArray(int* newArray){
    cout << sizeof(newArray);
} 


Where a value of 4 does make sense!?

And this holds for multi-dimensional arrays, too

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;

size_t getSizeOfArray(int newArray[3][4][5]); // creating a prototype

int main(){
    int myArray[2][4][5];
    cout << "sizeof         = " << sizeof(myArray) << endl;
    cout << "getSizeOfArray = " << getSizeOfArray(myArray) << endl;
}

size_t getSizeOfArray(int newArray[3][4][5]){
    return sizeof(newArray);
}


which outputs:

sizeof         = 160
getSizeOfArray = 4

Also note that I passed a 2 x 4 x 5 array to the function even though it was declared to take a int newArray[3][4][5] parameter; the size in the first [] is ignored.

But you can also pass an array by reference, like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;

size_t getSizeOfArray(int (&newArray)[3][4][5]); // creating a prototype

int main(){
    int myArray[3][4][5];
    cout << "sizeof         = " << sizeof(myArray) << endl;
    cout << "getSizeOfArray = " << getSizeOfArray(myArray) << endl;
}

size_t getSizeOfArray(int (&newArray)[3][4][5]){
    return sizeof(newArray);
}

This not only gives the correct array size, it also prevents you from passing an array other than (here) a 3 x 4 x 5 array (otherwise there is a compile error.)

sizeof         = 240
getSizeOfArray = 240

Andy
Last edited on Jun 15, 2013 at 9:59am
Jun 15, 2013 at 12:11pm
Thank you Andy.
I am currently a beginner in C++ and I don't have indepth knowledge about pointers.

But I'm sure that your answer will come in handy once I get good understanding of pointers.

-Himansh
Jun 15, 2013 at 9:08pm
:-)

There are a number of articles on arrays and pointers on this site, if you need some more info'

Andy

Pointers

Pointer craft
http://www.cplusplus.com/articles/z186b7Xj/

Pointers
http://www.cplusplus.com/articles/EN3hAqkS/

Using Pointers 3 dfferent ways
http://www.cplusplus.com/articles/D186b7Xj/

Pointers and References
http://www.cplusplus.com/articles/1075fSEw/

Distinguish between pointers and references in C++
http://www.cplusplus.com/articles/ENywvCM9/

C++ Pointer Basics (Part 1)
http://www.cplusplus.com/articles/Dz18T05o/

The Pointer
http://www.cplusplus.com/articles/ihTbqMoL/

Arrays

Multi-Dimensional Arrays
http://www.cplusplus.com/articles/NAUq5Di1/

Multidimentional arrays are evil
http://www.cplusplus.com/articles/G8hv0pDG/

Pointers vs Arrays

The difference between pointers and arrays
http://www.cplusplus.com/articles/NUCRko23/

Array is not pointer
http://www.cplusplus.com/articles/2TA0RXSz/

Jun 16, 2013 at 5:48am
Thanks for these links. I'll definitely check them out.
Topic archived. No new replies allowed.