I want to know how to declare C language 2D array + pointer array!

Asked 2 years ago, Updated 2 years ago, 75 views

int arr[2][3] = { {1,2,3},{4,5,6} }; 
int(*parr)[3];
parr = arr;

I'm not sure what int (*parr)[3]; means in the above code. The arr array declared in the first line consists of two rows and three rows (*parr)[3] does not have the address value of the first element of each row?

(Address output in hex) - As you can see, parr[0] has the same address as parr[0][0], parr[1] and parr[1][0]. However, parr[2] has an address after arr[1][2] that I didn't generate, and when I checked the value I had at that address, I confirmed that an uninitialized value came out. However, if you declare int (*parr)[2];, a compilation error occurs. I don't understand much about language c, so please explain it in detail It's quite complicated. Thank you!

c pointer 2d-array

2022-09-22 18:26

1 Answers

int (*parr)[3]; is a pointer to a two-dimensional array.

parr = arr; will point to an array of 2*3.

You can access the arr with a pointer as shown below.

[cling]$ parr[0]
(int [3]) { 1, 2, 3 }
[cling]$ parr[1]
(int [3]) { 4, 5, 6 }

And parr[2] goes beyond the range of 2*3 of arr, so you'll get an incorrect value. It's not worth the garbage.and so on

int (*parr)[2]; is a grammatical error in parr = arr;. Since the array is 2*3, 3 spaces are required. So it's a pointer to an array of three elements.

int **pa = (int**)arr You can hold addresses with double pointers, but the array is not accessible. Usually used to point to an array of pointers.

I can't write down the lecture now, but I'm attaching a well-organized address instead.

https://noname2.tistory.com/102


2022-09-22 18:26

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.