In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to use C++ array pointers and two-dimensional arrays". In daily operation, I believe many people have doubts about how to use C++ array pointers and two-dimensional arrays. I have consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "how to use C++ array pointers and two-dimensional arrays". Next, please follow the editor to study!
1. Two-dimensional array
For an one-dimensional array, int arr [10]; arr is the name of the array and the address of the first element, and & arr is the address of the array, so for the two-dimensional array int arr [3] [3], arr is definitely the name of the array, so is it the address of the first element? If it is the address of the first element, what should arr [0] be? Verify with the following code:
# include # define ROW 3#define COLUMN 3int main () {int arr [ROW] [COLUMN] = {0}; printf ("arr =% p\ n", arr); printf ("arr [0] =% p\ n", arr [0]); printf ("& arr [0] [0] =% p\ n", & arr [0] [0]); printf ("\ n\ n"); printf ("arr + 1 =% p\ n", arr + 1) Printf ("arr [0] + 1 =% p\ n", arr [0] + 1); printf ("& arr [0] [0] + 1 =% p\ n", & arr [0] [0] + 1); printf ("& arr [0] [1] =% p\ n", & arr [0] [1]); return 0;}
The result of the code running is:
It can be found that although arr = arr [0] = & arr [0] [0], arr [0] + 1 = & arr [0] [0] + 1 = & arr [0] [1]! = arr + 1, it shows that arr is not the address of arr [0] [0], but the address of arr [0]. So is arr still the address of the first element in a two-dimensional array? The answer is: yes, arr is the address of the first element, except that the first element in the two-dimensional array is not arr [0] [0], but arr [0]. Arr [0] is an one-dimensional array, so the address of the arr is the address of the array, so the arr can be saved through the array pointer.
So for a two-dimensional array like int arr [3] [5], how do you define an array pointer to hold the arr? That is,-- = arr;-- what should I fill in at the underscore?
The answer is: int (* parr) [5] = arr; because arr is a two-dimensional array, its first element is an array with five int elements, and arr is the address of the first element of the array, so arr is the address of an array.
So what should int (* parr [2]) [3] mean? First of all, [] has a high priority in (), so parr is an array with two elements, and there are also * in (), so the elements of the array are array pointers, and the array pointer points to three elements, each of which is of type int.
The code verification is as follows:
# include int main () {int arr1 [3] = {1int 2je 3}; int arr2 [3] = {2je 3jue 4}; int (* parr [2]) [3] = {& arr1,&arr2}; printf ("& arr1 =% p\ n&arr2 =% p\ n", & arr1,&arr2); for (int I = 0; I)
< 2; i++) { printf("parr[%d] = %p\n",i,parr[i]); } return 0;} 运行结果为: 2、数组指针和二维数组 再次强调一下,int arr[2][3],这个二维数组的首元素是arr[0] ,arr[0]是一个一维数组,arr是首元素的地址,就是一个数组指针,所以,int (*parr) [3] = arr; 这也是为什么二维数组定义的时候可以不给出行元素的个数,但是一定要给出列元素的个数。 #include int main(){ int arr[][3] = {{1,2,3},{1,2,3}}; //正确定义 int arr[2][] = {{1,2,3},{1,2,3}}; //错误定义 return 0;} 指针数组和二维数组在应用中常用于函数传参,如我们不用数组指针来实现一个二维数组的值修改函数, 代码如下: #include #define ROW 2#define COLUMN 3void arrReset(int arr[ROW][COLUMN], int row, int col) {for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { arr[i][j] = 0; } }}int main(){ int arr[ROW][COLUMN] = {{1,2,3},{1,2,3}}; arrReset(arr, ROW, COLUMN); for (int i = 0; i < ROW; i++) { for (int j = 0; j < COLUMN; j++) { printf("%d ", arr[i][j]); } printf("\n"); } return 0;} 以上代码函数传参是通过数组的形式实现,代码运行结果为:The parameters of two-dimensional array functions are usually passed through pointers to facilitate read and write operations. The following is how to pass parameters in the form of pointers:
1 # include 2 # define ROW 2 3 # define COLUMN 34 void arrReset (int (* parr) [COLUMN], int row, int col) {5 for (int I = 0; I < row; I +) {6 for (int j = 0; j < col; j +) {7 / / the following 8-11 lines of code are equivalent 8 / / parr [I] [j] = 0 9 / / (* (parr + I)) [j] = 0ROW 10 / * (* (parr + I) + j) = 0ROW 11 * (parr [I] + j) = 0ROW 12} 13} 14} 15 int main () 16 {17 int arr [ROW] [COLUMN] = {{1pm 2pm 3}, {1pm 2pm 3}; 18 arrReset (arr, ROW, COLUMN) 19 for (int I = 0; I < ROW; iTunes +) {20 for (int j = 0; j < COLUMN; jacks +) {21 printf ("% d", arr [I] [j]); 22} 23 printf ("\ n"); 24} 25 26 return
You can find code 8-11 behavior equivalent code, which is the same as the previous article array pointers and pointers array-low achiever Cash +-blog Park (cnblogs.com) blog where the pointer array access array elements of the same example, in fact, it can be understood that access to a two-dimensional array is to access an one-dimensional array of one-dimensional array. However, it should be noted that when accessing the elements of a two-dimensional array, it can be understood this way, but not when defining it, because one is a two-dimensional array and the other is an one-dimensional array.
Such as the following error code:
1 # include 2 int main () 3 {4 int arr1 [3] = {1include 2 int main 3}; 5 int arr2 [3] = {4 int arr2 5 return 6}; 6 int* arr3 [2] = {arr1, arr2}; 7 int arr4 [2] [3] = arr3; / / error code 8 9 return 0 position 10} this completes the study of "how to use C++ array pointers and two-dimensional arrays". I hope to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.