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 define a pointer and initialize it in C language

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

Share

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

This article introduces the knowledge of "how to define a pointer and initialize it in C language". In the operation of practical cases, many people will encounter this dilemma. Next, let the editor lead you to learn how to deal with these situations! I hope you can read it carefully and be able to achieve something!

In C language, after each variable is defined (Auto type definition), the compiler will open up memory space for them in the stack area of memory to store these variables. These memory spaces, whether contiguous or intermittent, are assigned different addresses. If these variables are introduced into the function as formal parameters, they are actually only passing the constants stored in the variables to the function, and the temporary variables inside the function are created when the function is initialized, which is not the same as the transfer variables where they are called, so no matter how to rewrite the contents passed in the formal parameters, it is essentially just to change the contents of these local variables inside the function.

Parameter transfer of function

When the pointer was designed from the beginning, it was not used to store a specific constant or variable, but for only one purpose, that is, the address of the corresponding variable, that is, the memory address of the variable. Once we have defined a variable, we can point the pointer to the variable. To point to a variable is to put the address of the variable in the pointer. When we use the pointer as the passing parameter of the data exchange function, it actually passes in the address of these two variables, and in the function, when we are exchanging the contents of these two variables, the value of the pointer variable passed into the function will not change, it will still store the address of the first two variables, but when we do the exchange operation, we put the existing variable in address An into address B. Put the variables that were originally stored in B into address A.

Parameter execution flow in the form of pointer variables

So, when we exchange the contents of the two variables, we use the pointer form parameters to pass the parameters. From the phenomenon, it seems that the contents of the two form parameters have been exchanged, but in essence, the contents of the pointer form parameters have not changed, only the contents of the memory pointed to by the two pointers have changed. Once you understand the pointer form parameters of this function, you will understand the following content very well. The definition of the pointer requires the use of a special symbol "*", which is the same as the multiplication operator in the operator, and you need to be careful when using it later. This "*" pointer operator has two meanings when doing pointer operations. let's start with the use of defining pointer variables. Pointer variables are defined as follows: data type * pointer variable name = initialization value; for example, define a pointer variable "a_pt" to point to the int type variable a. According to what is written above, it is easy to define a pointer variable called "a_pt", but the first step is to determine the data type of the defined pointer variable. In general, we define the corresponding type of variable as the type of variable we use this pointer variable to point to. The variable we are pointing to here is of type int, so the pointer variable is defined as type int. The second point to note is how to initialize this pointer variable to point to the variable a. As we said earlier, the ultimate purpose of the pointer variable is to store the address of the variable, so you only need to assign the address of the variable a to the pointer variable when the pointer variable "a_pt" is initialized. So how do I get the address of the variable a? You only need to precede the variable with the "&" symbol to return the address of the variable a. We can write a simple program, first define a variable a, then use the "&" symbol to get its address, and then print it out. Get the variable address and print it

After we have successfully obtained the variable address, we only need to assign the address directly to the pointer variable we defined. Definition of pointer variable

We define a pointer to the shaping type and point it to the variable a. Of course, depending on how we point to objects, we can also define simple data type pointers such as char type, long type, float type, and even complex data type pointers such as pointer arrays and structure pointers that will be learned later. "how to define a pointer and initialize it in C language" is here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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