explanation of a program

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#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;
}


result of this code is :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


What is the explanation of the output?Please help..trying to learn
b+1 is equal to address of b + sizeof(int) as b is a pointer to int
isn't 'b' a pointer to int[2]? So b+1 would be b + sizeof(int)*2

Anyway this is yet another reason I hate multidimentional arrays. They're syntax hell.

Here's the general rules:

- An array name without brakets is usually (but not always) considered a pointer to whatever the array is. IE: if you have:
1
2
3
4
5
6
7
8
9
10
int ar[5];  // 'ar' by itself would be a pointer to an 'int'
int* p = ar;  // so you can do stuff like this
p = &ar[0];  // functionally equivilent

// this is complicated by multidimentional arrays:
int ar[3][2];  // 'ar' actually points to int[2]

typedef int int2[2];  // make a typedef for int[2]  (not sure how do to this without it)
int2* pp = ar;
pp = &ar[0];  // functionally equivilent 


One of the exceptions to the above is with 'sizeof'. If you use an array name without brakets, sizeof computes the size of the entire array -- which differentiates it from a a pointer:
1
2
3
4
5
int foo[4];
int* bar = foo;

sizeof(foo);  // = sizeof(int)*4 --- (the size of the entire array)
sizeof(bar);  // = sizeof(int*)  ---  (the size of a single pointer) 



All that explained -- this is how the program you pasted works!

 
cout<<sizeof(b)<<endl; //  outputs 24 which is:  sizeof(int) * 2 * 3 


The reason for this is because 'b' is an array name. Therefore sizeof(b) computes the size of the entire array. This is sizeof(int)*2*3 or 24

cout<<sizeof(b+0)<<endl; // outputs 4 which is: sizeof(int2*)

The reason for this is because 'b' here is treated as a pointer (to an 'int[2]') because of the + operator. Therefore 'b+0' results in a pointer, and sizeof returns the size of the pointer (and not what the pointer points to)

cout<<sizeof(*(b+0))<<endl; // outputs 8, which is: sizeof( int2 );

For the same reason as above, b+0 results in a pointer to int2, and * dereferences that pointer to return the 'int2' being pointed to. This results in sizeof(int2) which is the same as sizeof(int)*2

cout<<"The address of b is: "<<b<<endl; // 0xbfec9710 address of array

'b' here is treated as a pointer to the start of the array. This address is equivilent to &b[0] or &b[0][0] (although all 3 are considered different types).

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

This 'b' is treated as a pointer to int2 here. The +1 points it to the next int2 in the array. This is the same address as &b[1][0]

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

This is another exception to the rule. Here, 'b' is not treated as a pointer, but is instead treated as a full array (like it is with sizeof). The & then converts it to a pointer of type int[3][2].

The difference is:
- 'b' points to int[2]
- '&b' points to int[3][2]

But they both ultimately point to the start of the array, which means they both output the same address.

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

&b returns a pointer to int[3][2], and the +1 makes the pointer point to the next (albeit nonexistant) int[3][2]. This essentially adds sizeof(b) to the address.


hope that clears it up! Sizeof and pointer math can be tricky and ugly! Multidimentional arrays only make it uglier!
Last edited on
Topic archived. No new replies allowed.