In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you about how to analyze the differences and relations between C++ arrays and pointers. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
Foreword:
For a long time, it has been said that data is a pointer when it comes to arrays and pointers, and this view has been accepted by more and more people. This article will focus on arrays and pointers. Is it the same for everyone to understand? That's all.
1 the concept of arrays and pointers
Array: the same set of data with fixed size and contiguous memory space. The stored elements have the same address continuity and data type.
Pointer: refers to the variable that stores the memory address. Start at 0. 32-bit systems can access 4G of memory, while 64-bit systems currently have 128g of addressable memory. (it could be bigger. This needs the support of hardware resources. Do not argue)
2 Operation of arrays and pointers 2.1 assignment
Array: assign values to array elements one by one.
Pointer: pointers of the same type can be assigned directly.
2.2 Storage
Array: you can know it from the definition of the array. The memory of the array is continuous. It can be defined on the stack or on the heap through malloc or new.
Pointer: itself is a variable, pointing to a variable of its corresponding type. The address you point to is also the address of the variable, and then get the value that points to the variable through that address.
2.3 Siz
It can be obtained through sizeof macros.
Array: the size of the array is obtained by sizeof (array name) / sizeof (type name).
Pointer: 4 in 32-bit operating systems and 8 in 64-bit operating systems.
2.4 initialization
The array is initialized as follows:
/ / initialize a double-precision array double balance [5] = {1.0,2.0,3.0,4.0,5.0}; / / initialize a two-dimensional array int a [5] [3] = {{80,75pr 92}, {61m 65pr 71}, {59pr 63je 70}, {85pr 87ct 90}, {76cr 7v 85}}
The pointer is initialized as follows:
/ define a pointer to an integer variable int* p=new int (0); / define a pointer to an array int* p=new int [n]; / define a pointer to a pointer int* * pp=new (int*) [2]; pp [0] = new int [6]; pp [1] = new int [10]
The definition of pointer array and array pointer please move to the following article: C language Concise knowledge Series 11 (part two): pointer
3 passing parameters of arrays and pointers
The parameter transfer mode of Cracket + can be divided into value type and address type. In fact, the parameter is copied by the value. Any modification to the parameter during the operation of the program is generated by the copied variable, and the input parameter will not be affected after the function exits. On the contrary, the addressing type will modify the original parameter. Arrays and pointers are basically referenced when they are passed as parameters, depending on how the code author uses them.
3.1 passing parameters of the array
Array parameters are divided into one-dimensional array and multi-dimensional array. It degenerates to a pointer when it is passed in as a parameter.
One-dimensional array:
Void Fun (int a []) {;} int main () {int testArray [] = {1pm 2pm 3}; Fun (testArray); return 0;}
As above, you can also define the parameter form of the Fun function as follows:
Void Fun (int * a) {;}
Two-dimensional array:
Void Fun (int (* a) [5]) {;} int main () {int testArray [2] [5] = {{1, 2, 3, 4, 5, 5}, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5
If the argument to the function is a pointer. Then there are three parameters that can be passed to the function, namely: the pointer variable, the address of the variable, and an array name. Here the array name actually points to the first address of the array.
Pointer:
Void Fun_p (int * p) {}; int main () {int testArray [] = {1Power2 testArray; int iValue 3}; int * pIntArray = testArray; int iValue = 10; / pass the array name Fun_p (testArray); / / pass the pointer variable Fun_p (pIntArray); / / variable address Fun_p (& iValue); return 0;}
Pointer of pointer:
Void Fun (int * * p) {;} int main () {int testArray [5] = {1, pIntArray 2, 3, 4, 5}; int * pIntArray = testArray; int iValue = 10; int * tmpP = & iValue; / / pass pointer variable Fun (& pIntArray); / / variable address Fun (& tmpP); return 0;}
In addition to the above, pointers also have function pointers, so naturally there will be an array of pointers to functions and pointers to function pointers. In practical use, the introduction of these types not only guides us to program flexibly, but also brings us great risks. Once something goes wrong, the complexity of troubleshooting and solving problems will also increase. You need to be cautious when programming pointers.
The above is the editor for you to share how to analyze the differences and relations between C++ arrays and pointers, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
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.