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;
}

Dani AI

Generated

The outputs come straight from C++ array-type rules and pointer arithmetic; the posts by and already point the way, so the following ties those observations together cleanly.

The variable is a 3-by-2 array of ints (six ints total), so sizeof(b) is the total bytes used by all elements — 6 × sizeof(int). When an array name is used in an expression like b+0 it decays to a pointer to a row (a pointer-to-array-of-2-int). That means sizeof(b+0) is the size of a pointer (platform dependent: 4 on 32-bit, 8 on 64-bit), while sizeof(*(b+0)) is the size of one row (2 ints).

That explains the printed addresses. Numerically b and &b yield the same base address, but their types differ: b (as a pointer) points to a row; &b is a pointer to the whole 3×2 array. Pointer arithmetic uses the pointed-to type size: b+1 advances by one row (2 ints), while &b+1 advances by the entire array (3×2 ints). In other words, if the base address is A then b+1 is A + row_size and &b+1 is A + total_array_size.

Quick, practical checks: compute row count with sizeof(b) / sizeof(b[0]) and column count with sizeof(b[0]) / sizeof(b[0][0]). Remember sizeof on a fixed array gives a compile-time byte count; pointer sizes and therefore results for pointer-based expressions depend on the compilation target (32-bit vs 64-bit). This ties together ’s memory layout and ’s sizeof explanation.

Recommended Answers

All 3 Replies

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.