Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use pointer variables in C language

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about how to use pointer variables in C language, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following contents. I hope you can get something according to this article.

After we have defined a pointer variable, the next question to consider is how to use the pointer variable. When we learned about ordinary variables, we know that the operation of a variable is actually divided into two steps: reading and writing. When pointer variables are usually used, they are mainly divided into two operation steps: read and write. Unlike conventional variables, the assignment operation of pointer variables is actually written to the address of an ordinary variable. We call this process the "pointing operation" of pointer variables. When reading a pointer variable, because the pointer variable stores the address of a variable, and the operation of reading an address is often meaningless, so when reading the pointer variable, in most cases we read the contents of the variable that the pointer variable points to. The pointing operation of a pointer variable is to point a defined pointer variable to a variable or memory region in the following way: "data type * pointer variable name = & point to the variable;" Of course, you can also define a pointer, and then point it to a variable, that is, "data type * pointer variable; pointer variable = & point to the variable;" when using, be sure to pay attention to the differences in the usage of the "*" sign above. It is also important to note that a pointer variable can only point to a variable of the same data type. Once a pointer points to a variable, manipulating the pointer variable is like manipulating the variable it points to. When operating, we read and write in the form of "* pointer variable name", which is equivalent to reading and writing to the variable pointed to by the pointer. The reading of the pointer variable to the variable is shown in figure 1.

Figure 1-pointer variable pointing to variable reading

At the same time, the assignment of the pointer variable to the variable is shown in figure 2.

Figure 2-assignment of pointer variables to variables

As we can see from figures 1 and 2, when the pointer variable p points to the variable a, use * p to operate as if you were manipulating the variable a. The above is the operation of pointer variables, very simple. It should be noted that when a pointer is defined, it must be initialized, that is, it points to the address of a variable, the pointer that does not point to any variable is called a "null pointer", and the null pointer cannot do anything. Once the null pointer is assigned, then the compiled program will not be able to use, as shown in figure 3.

Figure 3-pointer does not point to variable output error

This kind of pointer that does not point to any variable is called "wild pointer". Note that it is not a "null pointer". Null pointer and wild pointer are different pointers. Wild pointers are illegal and null pointers are legal. The difference between a null pointer and a wild pointer is that a wild pointer is a pointer that does not know where it points to. The main reason for its appearance is that the pointer is not initialized, or after the memory block pointed to by the pointer is released. The pointer is not set to a null pointer. Null pointer refers to the pointer to NULL. NULL is a macro defined by C language, which is 0. We will explain about null pointer in detail later. As we said earlier, the address of a variable is actively randomly assigned by the compiler when we define this variable, so if we randomly assign a value to a pointer variable after defining it, for the user, we don't know which memory address is available and which is not available. So is it illegal to do so? In fact, it is not. For example, when we are working on a single-chip microcomputer, you can find the address of a peripheral from the data manual, then we have to define a pointer to point to this address for use by subsequent programs. In this case, we must explicitly assign an address to the pointer variable. On the other hand, pointers can point anywhere at will, so if used improperly, such as pointing a pointer to some protected memory block and modifying this piece of content, it will cause some immeasurable errors. If you hastily assign a constant to a pointer, the compiler will be at a loss. In this situation, the compiler will only issue a warning, which means to assign a variable of type int to a variable of type (int *), as shown in figure 4.

Figure 4-assign a constant to the pointer

The right thing to do is that we need to cast this constant to a pointer type. Casting is actually very simple, just precede the variable or constant in parentheses with the type you want to convert. As shown in figure 5.

Figure 5-assign a constant address to the pointer

Now let's do an example, first define a variable a, assuming that the address of this variable an is the address of one of our hardware peripherals, we can first program to get its address, as shown in figure 6.

Figure 6-get the address of a variable

Next, let's define a pointer variable and explicitly assign the address to the variable, and then assign the memory to which the pointer points, and let's see if the contents of the final variable a will change accordingly. Since you are simulating hardware, there is a hint to note that we must modify this memory area with the keyword "volatile". As shown in figure 7.

After reading the above, do you have any further understanding of how to use pointer variables in C language? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report