What will the following code print Note the sizeof function

What will the following code print?

Note: the sizeof() function returns the number of bytes of the argument passed to it.

#include <iostream>

using namespace std;

int main( )

{ typedef int my_2darray[1][1];

my_2darray b[3][2];

cout<<sizeof(b)<<endl;

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

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

// the next line prints 0012FF4C

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

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

cout<<\"*(b+1) is: \"<<*(b+1)<<endl<<endl;

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

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

return 0;

}

Solution

#include <iostream>
using namespace std;
int main( )
{ typedef int my_2darray[1][1];
my_2darray b[3][2];
cout<<sizeof(b)<<endl; //it will return size of b i.e b is a type of int array=4 bytes,total element in b=6 therefore,ans=24
cout<<sizeof(b+0)<<endl; //size of first array,its a 2d array hence=2*4=8
cout<<sizeof(*(b+0))<<endl; //size of first array,its a 2d array hence=2*4=8
// the next line prints 0012FF4C
cout<<\"The address of b is: \"<<b<<endl; //address of b,could be different each time you execute the code
cout<<\"The address of b+1 is: \"<<b+1<<endl; //address of b+1,could be different each time you execute //the code
cout<<\"*(b+1) is: \"<<*(b+1)<<endl<<endl; //same as address of b+1
cout<<\"The address of &b is: \"<<&b<<endl; //same as address of b
cout<<\"The address of &b+1 is: \"<<&b+1<<endl<<endl; //address of &b+1,could be different each time //you execute the code
return 0;
}


*****OUTPUT**********

24
8   
8   
The address of b is: 0x7ffdb8976a90   
The address of b+1 is: 0x7ffdb8976a98   
*(b+1) is: 0x7ffdb8976a98   
  
The address of &b is: 0x7ffdb8976a90
The address of &b+1 is: 0x7ffdb8976aa8

*****OUTPUT**********

Please let me know in case of any doubt,thanks.

What will the following code print? Note: the sizeof() function returns the number of bytes of the argument passed to it. #include <iostream> using namesp
What will the following code print? Note: the sizeof() function returns the number of bytes of the argument passed to it. #include <iostream> using namesp

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site