In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how to use pointers in C language. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
1.1: Overview 1.1.1: memory
Memory meaning:
Storage: an important part used to store programs and data and to assist CPU in computing and processing.
Memory: internal memory that temporarily stores programs / data.
External memory: external storage, storing programs / data for a long time.
1.1.2: memory
Memory address:
It can be understood as abstracting memory into a large one-dimensional array.
Encoding is assigning each byte of memory to a 32-bit or 64-bit number. This memory number is called the address.
1.1.3: pointers and pointer variables
Each byte in memory has a number, which we call "address".
The essence of the pointer is the "address" of memory. The address is the pointer, and the pointer is the address.
The pointer is the address, and the defined pointer variable is the variable in which the address is stored.
1.2: pointer Basics 1.2.1: definition and use of pointer variables
-A pointer is a data type, and a pointer variable is a variable that holds an address number.
The'* 'dereference operator indicates the specific content of the memory space that the pointer refers to.
Print the address of the variable: (with% p)
The use of dereference operators:
Int * represents the pointer type, p is a variable of the defined pointer type, assigns the address of a to the variable P, which is the address number of a stored in p, p points to the space of a, and * P represents the content of space a, that is, the value of a.
1.2.2: modify the value of a variable indirectly through a pointer
We mentioned earlier about the modification of local variables modified by const, which cannot be directly modified by assignment. Here we introduce the indirect modification of the value of variables through pointers.
When we directly modify the constant modified by const, we can see that an error occurs.
At this point, we can define a pointer variable and indirectly modify the value of the local variable! Let's use a diagram to help us understand:
1.2.3: size of pointer
Sizeof () measures the size of the storage address that the pointer variable points to.
On a 32-bit platform, all pointer addresses are 32 bits (4 bytes).
On 64-bit platforms, all pointer addresses are 64 bits (8 bytes).
1.2.4: null pointer and wild pointer
Null pointer (NULL): mainly used to initialize pointer variables, null pointers point to the space with the memory address number 0. Int * P = NULL
Wild pointer: as far as I understand it, for wild pointer I understand it as pointing to an unknown space (generally speaking, although I know the address number of this piece of memory, but I don't know exactly what this piece of memory is stored in). Now I would like to introduce the common errors of the two field pointers.
①: any numerical value is assigned to the wild pointer.
②: the area of memory that the field pointer points to.
1.2.4: universal pointer
Without going into details here, the void * pointer can point to the memory space of any variable:
Special note: when we operate on the universal pointer, because the system needs to know how many bytes of content we need to operate on, so we need to convert to a specific pointer type.
Pointer variables modified by 1.2.5:const
When const modifies a pointer variable, the meaning of the modifier varies depending on the location of the const, as shown in the following code:
For p1, const can be understood as modified * p1, that is, the content of the modified space, when the address of this space can be re-assigned, but the content can not!
For p2Jet Const modifies the pointer variable p2, that is, the modified space address, so you can assist the content at this time, but can not change the address of the variable!
Note: when writing a program, the pointer is passed as a function parameter, and if the value of the corresponding memory space will not be modified, you can use const to modify the pointer data type.
1.3: pointer and array 1.3.1: array name
Hit the point: for an one-dimensional array, the array name is the address of the first element and is a constant! Cannot be modified, remember that for a two-dimensional array, the array name is also the address of the first element, except that the first element is no longer just an element, but an one-dimensional array.
1.3.2: pointer operation array
In fact, bloggers feel that the readability of operating arrays with pointers is still relatively poor, and in the subsequent face of multi-dimensional arrays, manipulating arrays with pointers is more troublesome and poor readability. But the blogger is not good at learning.
Here * (arr + I) has the same effect as arr [I]. The knowledge points here involve the addition and subtraction of pointers, as shown in the next section.
1.3.3: addition and subtraction of pointers
(1) addition operation
Pointer calculation is definitely not a simple addition of integers, it has something to do with the pointer type:
If it is an integer pointer, then the effect of + 1 is to change the size of an int.
If it is a character pointer, the effect of + 1 is to change the size of a char.
At this point, the array name is the address of the first element 1, which belongs to the integer pointer, and + 2 means to change the size of 2 int, that is, the address of the third element.
(2) subtraction operation
Here, the effect of minus an integer on a pointer is the same as that of addition, and it is also related to the type of pointer.
Here we use a reverse printing array to illustrate pointer subtraction, paying special attention to the difference between arr and & arr + 1. Arr represents the address of the first element, & arr represents the address of the entire array, arr + 1 changes 4 bytes, and & arr + 1 changes the number of bytes of the entire array size (36).
As shown in the figure:
We can see that the address numbers of print & arr and arr are the same, but the meaning is not the same.
1.3.4: pointer array
The pointer array is still an array, and the array elements are pointer types. Take the first-level pointer array as an example:
Here the blogger mentions his understanding of the first-level pointer array, hoping to be helpful to everyone. I correspond the first-level pointer array to the second-level pointer and two-dimensional array. I will not explain it in detail here. I will explain my understanding after pointer enhancement. This understanding may not be rigorous in official terms, but it can help us understand it.
1.4: summary of pointer basis
Int I: define integer variables
Int * p: defines a pointer variable to int
Int * * p: defines a second-level pointer to the first-level pointer.
Int arr [10]: defines an array of 10 integer elements.
Int * arr [10]: defines an array of 10 first-level pointer elements.
Thank you for reading! This is the end of the article on "how to use pointers in C language". I hope the above content can be of some help to you, so that you can learn more knowledge. If you think the article is good, you can share it for more people to see!
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.