Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two dimensional arrays and pointers

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

  1. As per my knowledge, board (i.e) array name represents the address of the first subarray (i.e) board[0] and

  2. board[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.

like image 915
intex0075 Avatar asked Dec 29 '11 15:12

intex0075


People also ask

How are pointers used in 2D array?

A Two Dimensional array of pointers is an array that has variables of pointer type. This means that the variables stored in the 2D array are such that each variable points to a particular address of some other element.

Is a 2D array the same as pointer to pointer?

An array is treated as a pointer that points to the first element of the array. 2D array is NOT equivalent to a double pointer! 2D array is "equivalent" to a "pointer to row".

What is the two-dimensional arrays?

A two-dimensional array is similar to a one-dimensional array, but it can be visualised as a grid (or table) with rows and columns. Many games use two dimensional arrays to plot the visual environment of a game.

What are two-dimensional array explain with example?

Here i and j are the size of the two dimensions, i.e i denotes the number of rows while j denotes the number of columns. Example: int A[10][20]; Here we declare a two-dimensional array in C, named A which has 10 rows and 20 columns.


2 Answers

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.

like image 148
clark Avatar answered Sep 25 '22 15:09

clark


As per my knowledge, board (i.e) array name represents the address of the first subarray (i.e) board[0]

This is only true if board is used outside of these contexts

  • As operand of the & operator
  • As operand of sizeof

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 of char(*)[3]). The same that is true about the array board is true about its first sub array board[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 example

// error: trying to apply '&' on something that has no address
&(1 ? board : board)

Note that anything said above applies to C; not necessarily to C++.

like image 44
Johannes Schaub - litb Avatar answered Sep 22 '22 15:09

Johannes Schaub - litb