What could be the possible explanation of this program output? I am trying to learn c++ so please help

#include <iostream>
using namespace std;

int main( )
{int b[3][2];

cout<<sizeof(b)<<endl;

cout<<sizeof(b+0)<<endl; 

cout<<sizeof(*(b+0))<<endl; 

// the next line prints 0012FF68

cout<<"The address of b is: "<<b<<endl;  

cout<<"The address of b+1 is: "<<b+1<<endl; 

cout<<"The address of &b is: "<<&b<<endl; 
;
cout<<"The address of &b+1 is: "<<&b+1<<endl<<endl;  

return 0;
}

This is the output:
24
4
8
The address of b is: 0xbfec9710
The address of b+1 is: 0xbfec9718
The address of &b is: 0xbfec9710
The address of &b+1 is: 0xbfec9728

These sound like teacher assigned problems for you to resolve so I am not going to give you the answer. But I will try to make the array more visual.

24 = sizeof( int b[3][2] );
XX
XX
XX

Array is 2 wide by 3 high. 3 times 2 is 6. So an array of 6 integers
24 / 6 = 4 So sizeof(int) = 4. Four bytes per integer thus 32-bit.
Okay that's the basics.
They are arrange in memory as follows:

0xbfec9710 [0][0] [0][1]
0xbfec9718 [1][0] [1][1]
0xbfec9720 [2][0] [2][1]

So when you take the size of b + 2
You are actually looking at the 3rd row starting at address of
0xbfec9720 that contains two integer elements.

The rest you should be able to figure out based upon this big visual hint!

Goodluck!

>>cout<<sizeof(b)<<endl;
The sizeof operator (its not a function!) returns the number of bytes occupied by the object. In the case of variable b it is 6 integers and the size of one integer is 4 bytes, so simple math 6 * 4 = 24.

The second one is a little more difficult to explain. If you print out the addresses then you will see that b+0 is the same address of b, and (b+1) is the same address as b[1];

0040FCE8
sizeof(b+0) = 4 0040FCE8 0040FCE8
sizeof(b+1) = 4 0040FCF0 0040FCF0
Press any key to continue . . .

int main()
{
    int b[3][2] = {1,2,3,4,5,6};
    cout << hex << b << "\n";
    cout << "sizeof(b+0) = " << sizeof(b+0) << " " << (b+0) << " " << b << "\n";
    cout << "sizeof(b+1) = " << sizeof(b+1)  << " " << (b+1) << " " << &b[1] << "\n";
}

Using the above program you should be able to figure out the rest.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.