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 analyze pointers in C language knowledge points

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

How to analyze the pointer in the C language knowledge point, I believe that many inexperienced people are at a loss about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Pointer is the soul of C, not learning pointer is equivalent to not learning C.

Definition of pointer: pointer is the number of memory unit (number > = 0)

A pointer variable is a variable that stores the number of a memory unit (the variable that stores the address)

Pointer & pointer variables are different

E.g: the address of the ordinary variable an is 1000H. You can say that the pointer of an is 1000H, but you can never say that the pointer variable of an is 1000H.

The pointer is a non-negative integer with limited operation: for example, the multiplication of two addresses is meaningless, so the multiplication cannot be done, so the pointer is limited.

Why are pointers important?

(1)。 Transfer data quickly and reduce the use of memory

(2)。 You can make the function return more than one value

(3)。 Direct access to physical hardware

(4)。 You can easily deal with strings.

(5)。 It is the basis of understanding the function of "reference" in object-oriented language.

(6)。 Can represent some complex data structures

Classification of pointers:

1. Basic type pointer (emphasis): int * iUnip / variable name: I int * indicates that the I variable holds the address of the variable of type int

E.g:

Int * i; int a = 10; I = a//error:i & an although they are all variables, but I is a pointer variable an is a normal variable type cannot be assigned I = & a Tabachap & is to take the address symbol & a represents the address of the variable a Assign the address of a to the pointer variable I printf ("* I =% d\ n", * I) / / output the result: * I = 3i after obtaining the address of a * I indicates that it points to a * I = a

The classic example deepens the understanding of pointers: swapping 2 numbers (previously using ordinary variables, now using pointers & functions)

# include void swap (int pjinint Q) {int t; t = p; p = Q; Q = t; return;} / / swap cannot be exchanged, but the actual parameter values in the main function of the interchangeable parameter are not interchangeable void swap1 (int * pjingint * Q) {int * t; t = p; p = Q; Q = t } / / swap1 can not complete the value exchange of a / b, but the pointer variable void swap2 (int * pdirection int * Q) {int t; t = * ptincinint * Q) {int main (void) {int aline b; a = 5; b = 9; swap (Azob) was successfully exchanged by int main (void). Printf ("a =% d ~ b =% d\ n", a ~ b); swap1 (& a =% d ~ b); printf ("a =% d ~ ~ b =% d\ n", a ~ b); swap2 (& a ~); printf ("a =% d ~ ~ b =% d\ n", a ~ b); return 0 } / * output result: a = 5 printf b = 9 a = 5 Magi b = 9 a = 9 r e b = 5 Summary: think: why can't you put it in the tuned function? The requirement of the question is: swap the value of a / b instead of the value of the modulated function. Test results: 1.swap is only the parameter value of the tuned function and the real disk in the main function is not swapped. 2.swap1 is not exchanged but the storage location of a / b address and the value in a / b address is not swapped * * Why is 3.swap2 successful? The reason is very simple. The function of swap2 is to swap the values in the a / b address. To put it simply: a / b is divided into two static addresses. 5. 9 is the value stored in the address. The swap2 function is to swap 59 of the two addresses. Once the value in the address reaches the swap, that is, the value of a / b completes the interchange.

After reading the above, have you mastered how to analyze the pointers in the C language knowledge points? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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

Development

Wechat

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

12
Report