array losing bytes

I do not understand what is happening here. Every time I check the size of the array, it seems to lose size.

The first cout in main says its 7, then the next says its 2, then the last says its 1. Ideas?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
lass sorting
{
    public:

    void mergeTwoSorted(int firstArray[], int secondArray[])
    {

        cout << sizeof(firstArray)/sizeof(int) << "\n";
        int a = 0;
        int b = 0;


        b = sizeof(firstArray)/sizeof(int)
        cout << firstArray[b];


/*


        for(int a=0; a < sizeof(firstArray); a++)
            cout << firstArray[a] << "\n";



*/
    }

};

int main()
{
        int  thisOne[7];
        int  otherOne[3];

        thisOne[0] = 2;
        thisOne[1] = 5;
        thisOne[2] = 6;
        thisOne[3] = 9;

        otherOne[0] = 1;
        otherOne[1] = 3;
        otherOne[2] = 7;

        sorting thisSorting;

        cout<< sizeof(thisOne)/sizeof(int) << "\n";
        thisSorting.mergeTwoSorted(thisOne, otherOne);


         return 0;
}
The sizeof trick only works when the compiler knows the size of the array at compile time. In this case, the compiler knows the size in main since it sees that thisOne is declared as int[7]. In the case of the function, the compiler only sees the declaration of firstArray as int[]. So the compiler treats firstArray as a pointer, so sizeof(firstArray) == sizeof(int*) == 4.

See here: http://www.cplusplus.com/forum/general/46751/

Topic archived. No new replies allowed.