In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail the example analysis of the comprehensive application of C language pointers. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
What is the pointer?
Pointer is not only an important concept in C language, but also an important feature of C language. Using it correctly and flexibly can make the program concise, compact and efficient. Everyone who learns and uses C language should learn and master pointers in depth. It can be said that not mastering pointers means not mastering the essence of C.
Pointer is the soul of C language (doge)
Since the desired variable unit can be found through the address, it can be said that the address points to the variable unit. For example, a room number 2008 is hung at the door of a room. This 2008 is the address of the room, or 2008 "points" to the room. Therefore, the address is visually referred to as a "pointer". It means that the memory unit with its address can be found through it!
Pointer variable
Examples of using pointer variables
An example accesses integer variables through pointer variables
Int main () {int a = 100, b = 10 / define the integer variable afield b and initialize int* p1, * p2; / / define the pointer variable p1Zero p2 to the integer data P1 = & a; / assign the address of the variable a to the pointer variable p1 p2 = & b; / assign the address of the variable a to the pointer variable p2 printf ("aversion% ddirection baked% d\ n", a, b); / / output the values of variables an and b printf ("* p1% djinjinp2% d\ n", * p1, * p2);}
Running result:
Two pointer variables p1 and p2 are defined at the beginning. But at this point, they do not point to any variable, but provide two pointer variables that specify which integer variable they can point to, and which integer variable to point to should be specified in the program. The function of the fifth and sixth line of the program is to make p1 point to an and p2 to b, where the value of p1 is & a (that is, the address of a) and the value of p2 is & b (that is, the address of b).
* Note: when defining a pointer variable, there should be a type name on the left, otherwise the pointer variable will not be defined.
For example:
Refer to an array through a pointer
The pointer to an array element is the address of the array element.
You can use a pointer variable to point to an array element. For example
Int arr [10] = {1, 3, 5, 5, 7, 11, 11, 13, 15, 17, 19}; / define an as an array of 10 integers int * p; / define p as a pointer variable to an integer variable, p _ pointer _ variable [0]; / / assign the address of a [0] element to the pointer variable p & array name vs array name.
Int arr [10]
We know that arr is the array name, and the array name represents the address of the element in the array.
So the question is & what exactly is the name of the arr array?
Why don't we have a piece of code!
It can be concluded that the address printed by the array name and the & array name is the same.
Let's look at another piece of code!
According to the code, we found that, in fact, arr and arr, although the value is the same, but the meaning should not be the same.
In fact, & arr represents the address of the array rather than the address of the first element of the array.
The address + 1 of the array skips the size of the entire array, so the difference between & arr+1 and & arr is 40.
Wild pointer
Concept: the wild pointer is the position where the pointer points to is unknown, (random, incorrect, undefined)
The cause of wild pointer 1. Pointer not initialized
two。 Pointer out of bounds access
How to avoid wild pointers
Pointer initialization
Watch out for the pointer crossing the line.
The pointer points to the space to release the NULL even though.
Avoid returning addresses of local variables
Check the validity before using the pointer
Pointer operation
When we were young, we knew how much one equals one, so let me ask you, if you add one to the pointer, do you add that number?
Let's look at a piece of code:
Here we can find that there is a difference of 1 between p and pendant 1.
How much is c here?
The decimal system of hexadecimal C is 12, so there is a difference of 4 between Q and Q.
P is the integer pointer equivalent to sizeof (int) = 4
Q is a character pointer equivalent to sizeof (char) = 1
This is the end of the article on "sample Analysis of the Comprehensive Application of C language pointers". I hope the above content can be of some help to you, so that you can learn more knowledge. If you think the article is good, please share it for more people to see.
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.