How do you read this line of code?

Forgive me for my poor English, me and my friend were doing school homework, suddenly he asked me to read this line of code ptr = &array[1][1][1] (ptr is a pointer to an integer).

He said

ptr is pointing to the address of the second element of the second
column of the second row of an array.

I think he will be right if it was an array of char. As far as it’s an integer 3D array I have no idea how to read that line of code in plain English.

How would you read it?

2

If you have 3 indexes, then it must be a three dimensional array. Since C only supports jagged arrays, a multidimensional array is simulated by a three-level jagged array. In other programming languages (like C#) a multidimensional array is one object having several dimensions. In order to access one element of the array you need as many indexes as there are dimensions.

A jagged array is effectively an array of arrays of arrays. It is a construction made up of many array objects linked together in a tree like manner. Since the top level array of a dynamically allocated array can contain references to other arrays of different lengths, it is called jagged array or ragged array. A statically allocated array is always rectangular. Example: Access an element in a 3-level jagged array:

int x = a[2][0][4];

Now, your expression is preceded by an & which means “address of” when used as unary operator. (Don’t confuse with the binary & operator which is the binary AND operator.)

The expression a[2][0][4] points to (or is the address of) an element of the array of the array of the array. Therefore &a[2][0][4] is a pointer to this address. It is common to think of two dimensional arrays as of rows and columns. Now each element in this row/column is again an array. Therefore you have a pointer to the address of an element of a column of a row of an array. In my example this is the fifth element of the first column of the third row. Arrays indexes are zero-based, therefore a[0] points to the first element.

“Pointer” is a high level term used in C, where as “address” is a low level term referring to the implementation. But both denote references to memory locations.

8

Let’s assume we have a 2d-array like (for simplicity sake, the process for higher dimensionality is the same)

int array[3][3];

The array viewed in memory is one dimensional, but logically we can think of it like this

array
----------------------------
| [0][0] | [0][1] | [0][2] |
----------------------------
| [1][0] | [1][1] | [1][2] |
----------------------------
| [2][0] | [2][1] | [2][2] |
----------------------------

When you call array[1][1], you are retrieving the value at that location (which I chose below to be 12)

----------------------------
|   0    |   0    |   0    |
----------------------------
|   0    |   12   |   0    |
----------------------------
|   0    |   0    |   0    |
----------------------------

This would allow us to construct code like the following

int value = array[1][1];
std::cout << value << std::endl;

Output

12

When you call &array[1][1], you are retrieving the pointer to that location

The following code would retrieve the pointer to the “center” of the array

int* ptr = &array[1][1];
(*ptr) = 10;

Which would change the value in that location to 10. This would result in the array to look like

----------------------------
|   0    |   0    |   0    |
----------------------------
|   0    |   10   |   0    |
----------------------------
|   0    |   0    |   0    |
----------------------------

Basically, the process for interpreting ptr = &array[1][1][1] is the same, but you are dealing with an array of higher dimensionality.

It is the pointer to the second element of the array in each dimension. Your teacher would’ve been correct if your code said ptr = &array[0][0][0]

If you have a threedimensional array, then rows and colums doesn’t apply, so I would say something like:

The variable ptr is assigned the address of the second item in the third dimension of the second item in the second dimension of the second item in the first dimension of the array.

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật