In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains the "detailed introduction of the pointer to the C language". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. let's study and learn the detailed introduction of the pointer to the C language.
Pointer of C language
The most notorious thing about C language is its use of pointers, which is also the essence of C language. When learning C language, many people complain that pointers are very difficult to learn and use. Today we are going to discuss the problem of C language pointer.
Introduction of pointer
One of the characteristics of C language is that everything is in memory, and memory is allocated to define any type of variable. Defining variables in a function requires unallocated memory on the stack; defining global variables requires allocating memory in the static memory area, and even defining functions also allocates memory in the static area (this is the so-called code segment storage area); memory that is not fixed in size needs to be allocated in the dynamic area.
With memory, you need to manipulate memory, that is, reading or writing memory data. The simplest memory operation of the C language is achieved through the assignment operator (=). For example, the following code, assigning a value to variable a, is actually changing the data of memory occupied by variable a; even the user-defined structural variable c, you can also directly use the assignment operator to copy the data of the block of memory occupied by b to the block of memory occupied by c to complete the read and write operation of memory. Of
Int assigns the values of all the members in b to the members in c by using the assignment operator, assigning the values of all the members in b to the members in c by assigning the values of all the members in b to the members in c by using the assignment operator, int a, b, m, b, m, b, b, m, b, c, b, b, C = b
So you can do this because variables in C language contain the address of memory (through & fetch) and the size of memory (fetched through sizeof). Knowing these two information, you can use memcpy to achieve assignment even if there is no assignment operator. For example, the following code does not use the assignment operator at all, but achieves the purpose of assignment.
# include static int g_test = 0 main () {int a = 1; / / here we use memcpy to operate memory to implement the assignment operation memcpy (& g_test, & a, sizeof (a)); return 0;}
But there is a problem here, even if the function parameters of C language are passed only by values, that is, all variables pass through parameters, there will be a copy, and the only way is to pass the address of the variable. this creates the need to use a special type to store the memory address of the variable, and the pointer arises at the historic moment, which has the following advantages:
Saving the memory address with a special type is more convenient than using the existing type (int, unsigned int), because the pointer determines the size of the memory address.
Syntax is introduced to facilitate the operation of memory: a statement like the above operation can solve int * addr = & groomtest; * addr = 1 This may not be obvious, but when there are many member variables in the structure, we have to calculate the offset of each member ourselves, and the pointer can directly find the address of each member, such as the-> symbol operation of the pointer. You can also use [] to offset memory.
Secondary pointer
C language introduced pointers, and derived a concept of secondary pointers, which directly confused some freshmen. In fact, secondary pointers are only the embodiment of grammar and are essentially pointers, which are used to store memory addresses. This essence has not changed. For example:
Int a = 0 the address of int p1 storage a * p1 = & the address of int * * p2 of p2 storage p1
All variables in C language are in memory and can be addressed, and pointer variables are no exception. No matter what level of pointer it is, it can be used as a first-level pointer, as long as you know the meaning of the current variable and deal with it, such as the following code:
Int alloc_mem (char * p, int size) {char * tmp_p = (char *) malloc (size); / / * p stores the memory address of `p1` or `p2` variable, and uses memcpy to pay the allocated memory address directly to the memory memcpy (p, & tmp_p, sizeof (char *)) pointed to p; return 0;} int alloc_mem1 (char * * p, int size) {* p = (char *) malloc (size) Return 0;} int main () {char * p1 = NULL; char * p2 = NULL; / / using alloc_mem ((char *) & p1,2) of c language; / / using second-level pointer alloc_mem1 (& p2,2);}
The above alloc_mem and alloc_mem1 effects are the same, except that alloc_mem1 uses the concept of secondary pointers.
Pointers and arrays
The essential difference between a pointer and an array owner is that the address used to store a piece of memory is strictly the starting address of a piece of memory. As for the size of this piece of memory, it is unknown and must be explicitly specified by the user. For example, the prototype of the function memcpy is as follows:
Void * memcpy (void * dst, const void * src, size_t n)
The dst pointer refers to the starting address of the target memory, but how much memory it points to must be given by the parameter n, and the responsible function cannot know whether it has overflowed. The array itself is a block of memory, including the starting address and size of the array, for example, we define an array:
/ / the starting address of the array is & arr, and the size is sizeof (arr) int arr [32]
The address of the arr array is & an and the size of the array is sizeof (arr). There is a problem here, that is, how to pass an array through function arguments. C language does not support passing an array, which converts the array to a pointer to the starting address of the array by default. So C language function transfer array, need to take the extra size of the array to prevent the problem of memory overflow. In fact, whenever you pass a pointer argument to a function, you have to bring this pointer to the size of the memory.
Is the pointer difficult to learn?
Most people think that pointers are difficult to learn, but not in the pointers themselves, but in what the pointers point to-memory. If the C language wants to save data, it can be allocated on the stack, and the scope of this part of the memory is limited to the inside of the function; the memory can be allocated in the static area, the memory scope of this area is global, but only limited to the memory of a certain size; memory can be allocated in the dynamic area, which is global and supports variable size memory. Most of the memory stored by C language programs is allocated in the dynamic area. This creates memory management problems, including single not limited to:
Allocation of memory
Release of memory
Memory size check
Handling of memory allocation failure
Boundary handling: characters always end with 0
Processing of reading out of bounds: reading data outside of valid memory
Write out-of-bounds processing: writing data outside of valid memory
Because of the above problems, C language is not as easy to use as java,python, because these languages have garbage collectors and do not need users to deal with memory problems. This is the root of the difficulty of learning C language.
Conclusion
The pointer is the data type that stores the memory address, and the C language can operate memory according to the memory address.
Pointers are different from arrays in that pointers store the starting address of memory, excluding the size of memory; arrays have both a starting address and a size.
Pass the pointer through the function, in addition to the pointer parameter itself, but also with the pointer to the size of the memory.
The difficulty of learning pointers is not the pointer itself, but the difficulty of memory management.
Thank you for your reading. the above is the content of "detailed introduction of the pointer of C language". After the study of this article, I believe you have a deeper understanding of the detailed introduction of the pointer of C language. the specific use of the situation also needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.