Question: c++ question about pointers with a 2D array. I will provide the code below. I thought I understood pointers fairly well, till I saw this
c++ question about pointers with a 2D array. I will provide the code below. I thought I understood pointers fairly well, till I saw this code. For line that states cout << sizeof(b), prints 24. I was told that char* b[2][3] is an array that holds 6 pointers of 4 bytes which we can conclude is 6*4 = 24. I agree that b has 6 pointers, but why of 4 bytes, isn't char 1 byte? Assuming it is 4 bytes all of the code bellow is correct. Maybe an answer with a picture will be helpfull, thank you for your help in advance. Also just for clarification can, char* b[2][3] be explained, thank you in advance.
#include
int main() {
char* b[2][3]; cout << sizeof(b) << endl;//prints 24 cout << sizeof(b + 0) << endl;//prints 4 cout << sizeof(*(b + 0)) << endl;// prints 12 //assume the new line prints 003EF7D0 cout << "the address of b is: " << b << endl;// prints 003EF7D0 cout << "the address of b + 1: " << b + 1 << endl;//prints 003EF7DC cout << "the address of &b is: " << &b << endl;// prints 003EF7D0 cout << "the address of &b + 1" << &b + 1 << endl;//prints 003EF7E8
return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
