In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
In this issue, the editor will bring you about how to deeply understand the C language pointer. 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.
Pointer is a data type pointer is also a variable, occupies memory space, used to save memory address pointer is to tell the compiler to open up 4 bytes of storage space (32-bit system), no matter how many levels of pointer are the same
* p operate memory
When the pointer is declared, the * sign indicates that the declared variable is the pointer. When the pointer is used, the * sign indicates that the value in the memory space pointed to by the operation pointer * p is equivalent to finding a piece of memory through the address (the value of the p variable). Then operate memory * p to assign values to the left of the equal sign (to assign values to memory) * p to the right of the equal sign (to get values from memory)
The pointer variable and the memory block it points to are two different concepts.
Assign p to p = 0x1111; it only changes the value of the pointer variable, not what it refers to. P = p + 1; ``pmarker + assigns a value to * p; does not change the value of the pointer variable, but only changes the value of the memory block referred to. The left side of the equals sign * p represents the value assigned to the memory, and the right side of the equals sign * p indicates a value. The meaning is completely different to the left side of the equals sign char * p (handle strings, the pointer points to the first address of the string: char * p = buf;p++. ) ensure that the memory block can be modified, that is, do not appear wild pointers and point to the system memory area, and modify the constant area data
A pointer is a data type, which refers to the data type of the memory space it points to.
Pointer step (pause +), determined according to the data type of the resulting memory space
Pendant + = (unsigned char) p + sizeof (a)
Conclusion: the step size of the pointer is determined according to the type of memory space.
Changing the value of a variable through * pbalance pinning + is the greatest meaning of the existence of a pointer.
Pointer variables and memory block variables it points to are completely different things.
The pointer points to a variable, that is, whether or not the address of a variable is given to the pointer is actually the address.
* p conditions for indirect assignment
Three conditions need to be met.
Two variables (usually an argument and a parameter) establish a relationship, and the actual parameter address is assigned to the parameter pointer * p parameter to indirectly modify the value of the parameter.
Int num = 0; / / argument int * p = NULL;p = & num;num = 1 / 2; / / change the value of argument indirectly through * parameter
Application scenario of indirect assignment
Within a function:
* p1room+ = * p2room+
Function call:
Int getStr (int * a) {}
When the function is called, use the n pointer (parameter) to change the value of the pointer (argument) of nmur1.
There are two ways to change the value of level 1 pointer (e.g. Char * p = 0x1111) and two ways to change the value of level 2 pointer (e.g. Char * * pp = 0x1111). When a function is called, the parameter is passed to the argument, the address is taken with the argument, the parameter is passed to the parameter, and * p is used in the called function to change the argument. The corollary of indirect assignment from the operation result = > the essence of function parameter pointer as function parameter pointer
Use level 1 pointer (parameter) to change the value of level 0 pointer (argument) (indirectly modify the value of argument through * p) use level 2 pointer (parameter) to change the value of level 1 pointer (parameter) (indirectly modify the value of argument through * p) use level 3 pointer (parameter) to change the value of level 2 pointer (argument) (modify the value of argument indirectly by * p) Value) use n-level pointer (parameter) to change the value of n-level 1 pointer (argument) (indirectly modify the value of argument by * p)
In-depth understanding of the pointer must be combined with the concept of four areas of memory, and pay attention to the input and output characteristics of the pointer.
# tone function modulated function
The main tone function can pass the heap area, stack area and global data memory address to the called function. The called function can only return the heap area and global data.
Memory allocation mode
Pointers are used as function parameters and have input and output characteristics.
Application pointers must be combined with function calls (pointers as function parameters)
1 1 level pointer (as input) heap allocation using general disabled stack allocation using commonly used 2 1 level pointers (as output) stack using outgoing results commonly used 3 2 level pointers (as input) heap allocation using general disabled stack allocation using commonly used 4 2 level pointers (as output) heap allocation commonly used, not recommended Generally, it is not commonly used to use allocation when it is converted to a level 25-3 pointer (for output) heap.
Level 1 pointer as input
Int showbuf (char * p); int showArray (int * p; int num)
Level 1 pointer as output
Int getLen (char * pFileName, int * pFileLen)
Level 2 pointer as input
Int main (int argc, char * args []); / / pointer array int shouMatrix (int [3] [4], int len); / / 2D array string
Level 2 pointer as output
Int getData (char * * data, int * dataLen); int getData_Free (void * data); int getData_Free (void * * data); / / avoid wild pointer
Level 3 pointer as output
Int getFileAllLine (char * * content, int * pLine); int getFileAllLine_Free (cahr * * content, int * pLine)
Next, let's have some practical information.
Complex type description
To understand pointers, there will be some more complex types, so let me first introduce how to fully understand a complex type. It is actually very simple to understand a complex type. There are many operators in a type. Like ordinary expressions, they also have priority, and their priority is the same as that of operation, so I summed up the principle: starting from the variable name, combining according to operator priority. Analyze step by step. Let's start with a simple type and analyze it slowly:
Int p; / / this is a common integer variable int * p; / / starts from P and combines with * first, so P is a pointer, and then combined with int, indicating that the type of content pointed to by the pointer is int. So P is a pointer int p [3] that returns integer data; / / it starts at P and combines it with [] to show that P is an array, and then combines with int to show that the elements in the array are integer, so P is an array composed of integer data int * p [3]. / / first, start from P and combine with []. Because its priority is higher than *, P is an array, and then it is combined with * to show that the elements in the array are of pointer type, and then combined with int to show that the type of content pointed to by the pointer is integer, so P is an array int (* p) [3] consisting of pointers that return integer data. / / first, starting from P, combine with * to show that P is a pointer and then combine with [] (with "()" this step can be ignored, just to change the priority), indicating that the content pointed to by the pointer is an array, and then combined with int to show that the elements in the array are integer. So P is a pointer int * * p to an array of integer data; / / first, starting with P, combining with *, saying that P is a pointer, and then combining with *, indicating that the element pointed to by the pointer is a pointer, and then combining with int, indicating that the element pointed to by the pointer is integer data. Since secondary pointers and more advanced pointers are rarely used in complex types, we will not consider multi-level pointers for more complex types, but only one-level pointers at most. Int p (int); / / from P, first combine with () to show that P is a function, and then enter the analysis in () to show that the function has a parameter of an integer variable, and then combine with the outer int, indicating that the return value of the function is an integer data Int (* p) (int). / / starting from P, it is first combined with the pointer to indicate that P is a pointer, and then combined with () to indicate that the pointer points to a function, and then combined with the int in () to indicate that the function has a parameter of int type, and then combined with the outermost int, indicating that the return type of the function is an integer. So P is a pointer int * (* p (int)) [3] to a function that has an integer parameter and returns an integer type. / / you can skip it first, without looking at this type. If it is too complicated, start with P, combine with (), show that P is a function, then enter (), combine with int, show that the function has an integer variable parameter, and then combine with the outer *, indicating that the function returns a pointer, and then go to the outermost layer and combine it with [] first to show that the returned pointer points to an array. Then combine with * to show that the elements in the array are pointers, and then combine with int to show that the pointer points to integer data. So P is a function whose argument is an integer and returns a pointer variable that points to an array of integer pointer variables.
That's about it. That's all we have to do. After understanding these types, other types are also a piece of cake for us, but we generally don't use too complex types, which will greatly reduce the readability of the program. Please use it with caution, the above types are enough for us.
I. elaborate on the pointer
A pointer is a special variable in which the value stored is interpreted as an address in memory. To figure out a pointer, you need to understand four aspects of the pointer: the type of the pointer, the type the pointer points to, the value of the pointer or the area of memory that the pointer points to, and the area of memory occupied by the pointer itself. Let's explain separately.
First declare a few pointers as examples:
Example 1:
(1) int*ptr; (2) char*ptr; (3) int**ptr; (4) int (* ptr) [3]; (5) int* (* ptr) [4]
Serial number pointer stack main tune function parameter tuned function parameter remark
1. Type of pointer
From a grammatical point of view, you just need to remove the pointer name from the pointer declaration statement, and the rest is the type of pointer. This is the type of pointer itself. Let's look at the types of pointers in example 1: (1) the type of int*ptr;// pointer is int* (2) the type of char*ptr;// pointer is char* (3) the type of int**ptr;// pointer is int** (4) int (* ptr) [3]; / / the type of pointer is int (*) [3] (5) int* (* ptr) [4]; / / the type of pointer is int* (*) [4]? Is it easy to find out the type of pointer?
two。 The type that the pointer points to
When you use a pointer to access the area of memory that the pointer points to, the type the pointer points to determines what the compiler will think of the contents of that area of memory. Syntactically, you only need to remove the pointer name in the pointer declaration statement and the pointer declaration * to the left of the name, and all that is left is the type that the pointer points to. For example: (1) the type pointed by the int*ptr; / / pointer is int (2) the type pointed by the char*ptr; / / pointer is char (3) the type pointed by the int**ptr; / / pointer is int* (4) int (* ptr) [3]; / / the type pointed to by the pointer is int () [3] (5) int* (* ptr) [4]; / / the type pointed to by the pointer is int* () [4]
The type that the pointer points to plays an important role in the arithmetic operation of the pointer. The type of the pointer (that is, the type of the pointer itself) and the type the pointer points to are two concepts. As you become more and more familiar with C, you will find that dividing the concept of "type" mixed with pointers into "types of pointers" and "types of pointers" is one of the key points for mastering pointers. I read a lot of books and found that in some poorly written books, the two concepts of the pointer were mixed together, so I became more and more confused when I read the book.
3. The value of the pointer-or the memory area or address that the pointer points to
The value of the pointer is the value stored by the pointer itself, and this value will be treated by the compiler as an address rather than a normal value. In 32-bit programs, the value of all types of pointers is a 32-bit integer, because memory addresses in 32-bit programs are all 32 bits long. The area of memory that the pointer points to is a piece of memory whose length is si zeof (the type the pointer points to), starting from the memory address represented by the value of the pointer. Later, when we say that the value of a pointer is XX, it is equivalent to saying that the pointer points to a memory area headed by XX; when we say that a pointer points to a memory area, it means that the value of the pointer is the first address of that memory area. The memory area that the pointer points to and the type that the pointer points to are two completely different concepts. In example 1, the type that the pointer points to already exists, but because the pointer has not been initialized, the area of memory it points to does not exist, or is meaningless. In the future, every time you encounter a pointer, you should ask: what is the type of this pointer? What is the type of pointer? Where does the pointer point? (key note)
4 the memory area occupied by the pointer itself
How much memory does the pointer itself take up? You only need to test it with the function sizeof (the type of pointer). On a 32-bit platform, the pointer itself occupies a length of 4 bytes. The concept of memory occupied by the pointer itself is useful in determining whether a pointer expression (explained later) is a left value.
Second, the arithmetic operation of pointer
The pointer can add or subtract an integer. The meaning of this operation of the pointer is different from that of the usual numerical addition and subtraction, in units. For example: example 2:
Char a [20]; int * ptr= (int *) a; / / casting does not change the type ptr++ of a
In the above example, the pointer ptr is of type int*, and it points to type int, which is initialized to point to the integer variable a. In the next third sentence, the pointer ptr is added 1, and the compiler handles it like this: it adds sizeof (int) to the value of the pointer ptr, which is added 4 in 32-bit programs, because int takes up 4 bytes in 32-bit programs. Because the address is in bytes, the address pointed to by ptr is increased by 4 bytes from the address of the original variable a to the high address. Because the length of the char type is one byte, it turns out that ptr is the four bytes that point to the beginning of unit 0 of the array a, which now points to the four bytes of the array a starting from unit 4. We can iterate through an array with a pointer and a loop. Look at the example:
Example 3:
Int array [20] = {0}; int * ptr=array; for (iDefino ptr-; / / pointing to the operator, or (* ptr) .a, it is recommended that the former be used as ptr- > b TX PTR-> c
And how to access the three member variables of ss through the pointer pstr? Answer:
* pstr; / / visited a member of ss. * (pstr+1); / / visited member b of ss. * (pstr+2) / / visited member c of ss.
Although I have called up the above code on my MSVC++6.0, you should know that it is irregular to use pstr to access structure members. To illustrate why it is irregular, let's look at how to access each unit of an array through a pointer: (change the structure to an array)
Example 13:
Int array [3] = {35 pa=array; / / access to the three units of the array array through the pointer pa is: * pa; / / accessed Unit 0 * (pa+1); / / visited Unit 1 * (pa+2); / / visited Unit 2
In terms of format, it is the same as the informal method of accessing structure members through pointers. When arranging the cells of an array, all the C _. However, when storing the members of the structure object, in a certain compilation environment, word alignment or double-word alignment or some other alignment may be required, and several "padding bytes" need to be added between the two adjacent members. this results in a gap of several bytes between the members. Therefore, in example 12, even if * pstr accesses the first member variable an of the structure object ss, there is no guarantee that * (pstr+1) will access structure member b. Because there may be several padding bytes between member an and member b, maybe * (pstr+1) happens to access these padding bytes. This also proves the flexibility of pointers. If your goal is to see if there are any padded bytes between the members of the structure, hey, that's a good idea. However, the correct way for pointers to access structure members should be to use the pointer ptr in example 12.
The relationship between pointers and functions
You can declare a pointer as a pointer to a function.
Int fun1 (char *, int); int (* pfun1) (char *, int); pfun1=fun1;int a = (* pfun1) ("abcdefg", 7); / / call the function through the function pointer.
You can use a pointer as a formal parameter of a function. In a function call statement, you can use a pointer expression as an argument. Example 14:
Int array [20] = {0}; int * ptr=array; for
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.