In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "what is the reason for the use of small knowledge in C language". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. let's study and learn "what is the reason for the use of pointers in C language?"
First of all, from a simple example, write a piece of code to exchange the values of x and y.
Void main (void) {U8 x = 10, y = 20; U8 temp; _ asm ("sim"); / / do not interrupt SysClkInit (); delay_init (16); LED_GPIO_Init (); Uart1_IO_Init (); Uart1_Init (9600); ADC_GPIO_Init (); _ _ asm ("rim") / / turn on interrupt while (1) {LED = ~ LED; printf ("x =% d\ r\ n", x, y); temp = x; x = y; y = temp; printf ("--> x =% d\ r\ n\ r\ n\ n\ n", x, y) Delay_ms;}}
In the STM8 single chip microcomputer, the values of x and y are exchanged and printed out, and the printed results are as follows:
The values of x and y are easily swapped through the third variable temp.
But for the sake of the aesthetics of the program, I didn't want to write so much code in the main program, so I decided to use a function to implement this function on the outside.
Void swap (U8 x, U8 y) {U8 temp; temp = x; x = y; y = temp;} void main (void) {U8 x = 10, y = 20; _ _ asm ("sim"); / / No interrupts SysClkInit (); delay_init (16); LED_GPIO_Init (); Uart1_IO_Init (); Uart1_Init (9600) ADC_GPIO_Init (); _ _ asm ("rim"); / / enable interrupt while (1) {LED = ~ LED; printf ("x =% d\ r\ n", x, y); sawp (x, y) Printf ("- > x =% dMagney =% d\ r\ n\ r\ n\ n", x, y); delay_ms (200);}}
Define a function outside the main function that is specifically used to exchange the value of XBI y. The running result of the program is as follows:
At this point, it is found that the values of x and y are not exchanged. What is going on? So inside the exchange function, the values of x and y are also printed out. Modify the code as follows:
Void swap (U8 x, U8 y) {U8 temp; printf ("in: X =% d\ r\ n", x, y); temp = x; x = y; y = temp; printf ("in:-- > x =% d\ r\ n\ n\ n", x, y);}
The print result is as follows:
You can see that inside the exchange function, the values of x and y have been exchanged, but outside the function, the values of x and y have not been exchanged. Why is that? Single-step debugging directly observe the partial storage of x and y in the single-chip microcomputer.
After entering the main program, first observe the storage of x and y in the main function in memory, the x value is 10, that is, hexadecimal 0x0A, stored in the location of 0x000009 in memory, the value of y is 20, that is, hexadecimal 0x14, 0x00000B in memory, location storage.
Next, go to the swap function. To facilitate observation, the x and y inside the swap function are replaced with m and n.
You can see that after entering the subfunction, the addresses and values of m and n are the same as x and y in the main function. Next, exchange the values of m and n.
After the swap is complete, it is found that the value of the 0x000009 location in memory is swapped with the value of the 0x00000B location. Then exit the subfunction and return to the main function.
At this time, something strange happened, and the value swapped in memory changed back? Why is that? Here, we have to say that there is no fixed storage location for local variables in C language. Local variables are managed uniformly by the stack of the system. When entering the function, the system temporarily allocates a storage space for these local variables to store their values. When the program leaves the function, the system saves the values of local variables in the stack. Then the storage location of the variable is released. When the program enters another function, it allocates space for the local variables inside the function, so it may appear that the local variables in both functions use the same memory space. When the value of this memory space changes, it does not affect the value of the local variable in the previous function, because the local variable value in the previous function is now stored in the stack. When the program is about to leave the current function, the current local variable value is saved in the stack. After going back to the previous function, the value stored in the stack is restored to the variable, and the address of the variable is applied temporarily, and the address value applied for now may be the same as the last time. But that doesn't mean the address belongs to the local variable forever.
This is very similar to the lockers in the supermarket. if you want to go into the supermarket to buy things, you first store things in a locker, and then take them out after shopping. Then after a few hours, I have to go shopping in this supermarket and save things, but the cabinet number is the same as the last time. But this does not mean that the number of this cabinet belongs to you. It is only the space temporarily allocated to you by the locker, which will be taken back by the system when you take out something, and will be automatically allocated to you if you need it next time, but these two times are just the same number.
So how to solve the problem of variable exchange? There are two ways, the first is to directly define the two variables to be exchanged as global variables, leaving it to occupy an address space while the program is running, so that no other variables will use this location. But in this way, it will be a waste of memory space, only used once, but it has to be permanently occupied. The second way is to use pointers directly.
Let's change the code to use pointers.
Void swap (U8 * m, U8 * n) {U8 temp; temp = * m; * m = * n; * n = temp;} void main (void) {U8 x = 10, y = 20; _ _ asm ("sim"); / / disable interrupting SysClkInit (); delay_init (16); LED_GPIO_Init (); Uart1_IO_Init () Uart1_Init (9600); ADC_GPIO_Init (); _ _ asm ("rim"); / / enable interrupt while (1) {LED = ~ LED; printf ("x =% d ·r\ n", x, y); sawp (& x, & y) Printf ("- > x =% dMagney =% d\ r\ n\ r\ n\ n", x, y); delay_ms (200);}}
You need to use the & symbol when passing parameters to the swap function. Print out the result
At this point, the values of x and y have been successfully exchanged. Now also print the exchange within the subfunction.
Void swap (U8 * m, U8 * n) {U8 temp; printf ("in: M =% d\ r\ n", m, n); temp = * m; * m = * n; * n = temp; printf ("in:-- > m =% d ~ n n =% d\ r\ r\ n\ n", m, n);}
From the results of the output, the values of m and n are 1021 and 1020 respectively. What is this value? Just try it step by step.
At this point, the address of x in the main function becomes 0x0003FDfuroy and the address becomes 0x0003FC. Then go into the subfunction.
At this point, we can see that the value of m is 0x03FDFDFor n is 0x03FC.*m, and the value of 0x0A is 10, which is 0x14, which means 20.
Next, start exchanging values.
You can see that the values of m and n have not changed, but the values of m and n have been exchanged. Let's go back to the main function.
At this point, the values of x and y in the main function are also exchanged.
What are the 1021 and 1020 printed by the serial port just now? The hexadecimal of 1021 is 0x03 FDD1020 and the hexadecimal of 1020 is 0x03FC. That is to say, the value of m just printed is the same as the address of x, and the value of n is the same as the address of y.
So why do m and n become x and y addresses, and * m and * n become values of x and y? Here we are going to talk about the nature of the pointer. Inside the memory, it does not recognize any variables and pointers, and for storage space, it has only addresses and values. That is to say, at what address, what value is stored. For ordinary variables, the name of the variable is compiled to an address by the compiler, which means that x and y are aliases for their own addresses. The values of x and y are the corresponding values in the address.
When manipulating the ordinary variables x and y, the system defaults to its value. The pointer is just the opposite, the pointer defaults to the address as its value, and when operating the pointer, the default operation is the address. In order to distinguish between ordinary variables and pointers, if you want to use a pointer, you need to label it and tell the system that this is a special variable and that it operates directly on the address, not on the value.
So when you define the pointer, adding a * sign to the variable means telling the system that this is special. For example, an int * m is defined. It means to tell the system that when I operate m by default, you give me its address, not its value. When you need to take a value, you need to add the label * m, tell the system that I want to take the value now, not the address, this is a special case, do not give me the default address.
When you want to pass an ordinary variable to a pointer, because the direct manipulation variable defaults to the value of the ordinary variable, and the pointer stores the address, so when the ordinary variable and the pointer pass data, you should also add a label to the ordinary variable. This symbol tells the system that I don't want the default value now. I want the address of a special case.
So when you pass x and y to the pointer, add the & symbol in front of it.
Swap (& x, & y); corresponds to swap (U8 * m, U8 * n)
As I just said, the pointer defaults to the address, and adding the * sign is the value. So isn't it the equivalent of * m = & x to pass it directly?
Since this subfunction is defined and operated together with passing values, one step is omitted, and the standard operation should be.
Int * m
Mechatronx
First define a pointer, and then pass the special case of the ordinary variable, that is, the address of the ordinary variable, to the default case of the pointer. So by default, m represents the address of the value x, and the value of x is * m.
If you define a variable and assign a value to a variable in the same statement, the above code can be abbreviated to
Int * m = & x
So the above function swap (& x, & y); when passing a value to the pointer, the pointer is defined and assigned in a statement, swap (U8 * m, U8 * n); this is a commonly used shorthand form.
Operating * m inside the swap function is equivalent to directly operating x, and operating * n is directly operating y, so exchanging the values of * m and * n is equivalent to exchanging the values of x and y.
When the pointer is not used, through the subfunction exchange, the value of the variable is passed, which is equivalent to copying the value of the variable and giving it to the subroutine.
With the pointer, it is equivalent to giving the address of the variable directly to the subfunction. It is equivalent to giving another alias to the variables x and y. When operating an alias, it is equivalent to directly manipulating x.
Thus it can be seen that the pointer is just a way to give a variable an alias to facilitate programming, that is, it is equivalent to a key to one's own cabinet. As long as someone else has this key, you can open your locker. So the pointer can be dangerous when used.
If there is critical data in the system, then if the data is passed to an external function with a pointer, then the system is at risk when the external function modifies the data. It is possible that the external function modifies a value, which is illegal, and its own system collapses. So be sure to pay attention to security when using pointers.
Through the above example, I believe that we will have a deeper understanding of the pointer. It is a special case set to facilitate the manipulation of variables, and it is a labeled variable. As for what pointer variables, variable pointers, those are just names, do not understand these concepts do not need to struggle, as long as in use, know how to use it.
Thank you for your reading. the above is the content of "what is the reason for the use of small knowledge of C language?" after the study of this article, I believe you have a deeper understanding of what is the reason for the use of pointer of small knowledge 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.