I have the following code snippet:
char board[3][3] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'}
};
printf("address of board : %p\n", &board);
printf("address of board[0] : %p\n", &board[0]);
Both printf()
statements all print the same value: 0x0013ff67
As per my knowledge, board (i.e) array name represents the address of the first subarray (i.e)
board[0]
andboard[0]
represents the address of first element in the first array (i.e)board[0][0]
Why am I getting the same address in all my printf()
statements? I expect different addresses for both statements.
I am pretty new to this stuff and don't understand this behavior. Kindly enlighten me.
This is only true if
board
is used outside of these contexts&
operatorsizeof
When any of that applies, expression
board
represents the array and keeps having the type of the array (char[3][3]
). Applying the&
operator to it results in getting the address of the array, which of course equals the address of its first element, merely having a different type (char(*)[3][3]
instead ofchar(*)[3]
). The same that is true about the arrayboard
is true about its first sub arrayboard[0]
.When you use it outside of those contexts, you get the address of the first element (subarray in your case). That address is not an object but just a value. Value have no address, but objects have. Trying to apply
&
on it would fail. For exampleNote that anything said above applies to C; not necessarily to C++.
Though it's a 2D array, inside the memory it will be represented as linear array. so when you say, board[0][0] it still points to the first element in that linear array and hence the same memory address.
It may be the case that you know an object-oriented language such as Java or Python, and now you are learning the C language. The difference between Java and C when thinking about
char board[3][3]
is that in C theboard
variable is represented in memory as 9 characters at adjacent memory addresses. Like so:In C,
&board
yields the same memory address as&board[0]
and&board[0][0]
.In contrast to this, in Java the variable would be declared as
char[][] board
and its memory representation would conceptually look like this:where
ptr(x)
points to memory address ofx
. So, in Java,board
points to a different memory address thanboard[0]
.Although the expressions
&board
and&board[0]
and&board[0][0]
yield the same address, the type system of the C language is preventing you from accessing thechar
value. In a C compiler, the types are (conceptually):Assuming a variable of type
char
, we can write:but cannot write:
because the type on the left side is incompatible with the type on the right side of the assignment.
If you are sure that an address points to a
char
, you can use a type cast:The downside is that such type casts may lead to coding bugs.
Of course this will print the same address.
Think about 2D arrays like this for a minute,
The address
*ptr
does equal&(**ptr)
.Because basically, that's what your code is doing.
and remember that in memory, 2D arrays will be mapped linearly.