In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to use pointers in C language". In daily operation, I believe many people have doubts about how to use pointers in C language. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the question of "how to use pointers in C language"! Next, please follow the editor to study!
I. the size of the pointer
Wild pointer and null pointer
# include int main (void) {int a = 6 position intt * p * p = a position / assign the value of a to the pointer variable p p is a wild pointer, this will not be a problem, but it does not make sense p = 0x456 pm / assign a value to the pointer variable p, p is a wild pointer, this will not be a problem, but it does not make sense * p = 100X pointer / the wild pointer points to an unknown area, memory will have problems and errors will be reported. Return 0;}
However, both wild pointer and valid pointer variables hold numeric values. In order to mark that this pointer variable does not point to any variable (free), you can assign NULL to this pointer in C language, thus indicating that the pointer is null and there is no pointer.
Int* p = NULL
Description: the wild pointer points to an unknown space, and the wild pointer is allowed in the program. The operating system uses 0 to 255 as the system occupies no access operations, and the memory space corresponding to the operation field pointer may report an error.
# include int main (void) {/ / Null pointer refers to the space with memory address number 0 int* p = NULL;// the space corresponding to the null pointer must report an error * p = 6; / / this will cause an error printf ("% d\ n", * p); return 0;}
Universal pointer void*
The void* pointer can point to the memory space of any variable:
# include int main (void) {/ / void* p = NULL;void* pashing int a = 6 p = (void*) & a; / / when pointing to a variable, it is best to convert it to void* / / when using a pointer variable to point to memory, convert it to int** ((int*) p) = 10; / / change the value of a to 10 / / when you change the value of a variable through a universal pointer, you need to find the pointer type corresponding to the variable. / / printf ("% p\ n", p); printf ("a =% d\ n", a); return 0;}
Program execution result:
The universal pointer can receive the memory address of any type of variable.
Void* p = & a
Fourth, pointer variables modified by const
(1) const modifier pointer type
Const int* p = & a
In this case: you can change the value of the pointer variable, not the value of the memory space that the pointer points to.
# include int main (void) {int a = 3tint b = 6 X Const int* p = & a; / / the initial value of p1 is the address of a p = & b; / / assign the address of b to p1, that is, change the value of p1 (this is feasible) * p = 15; / / error, indicating that the expression must be a modifiable left value, because the value of the memory space pointed to by the pointer cannot be changed. Printf ("% p\ n", p); / / the value of p1 printed out is the address of b, that is, the value of the pointer variable can be modified. Return 0;}
(2) const modifies pointer variables
Int* const p = & a
In this case: you can change the value of the memory space that the pointer points to, not the value of the pointer variable.
# include int main (void) {int a = 3nint b = 6; int* const p = & a * p = 100 * p = & b *. That is, the value of the pointer variable printf ("% d\ n", a) cannot be modified; / / the value of a printed out is 100, that is, the value of the memory space pointed to by the pointer can be modified, return 0;}
(3) const modifies pointer variables and pointer types
Const int* const p = & a
We know that in this case, you can change neither the value of the memory space that the pointer points to, nor the value of the pointer variable. However, we can still modify the value of the memory space and the value of the pointer variable through the secondary pointer.
# include int main (void) {int a = 2int b = 6; const int* const p = & an intact * pp = & p; * * pp = 100; / / assign 100 to a, that is, the value of a can be modified through the secondary pointer, and const does not work printf ("% d\ n", * p); / / the printed value of an is 100 * pp = & b / / assign the address of b to p, the value of p can be changed through the secondary pointer, const does not work printf ("% d\ n", * p); / / what is printed is the value of b, that is, 6 return 0;}
This also shows that there is some insecurity in const.
Fifth, addition operation of pointer
(1) pointer calculation is not a simple addition of integers.
(2) if the result of an int*,+1 is to increase the size of an int.
(3) if the result of a char*,+1 is to increase the size of a char.
Pointer subtraction is similar and easy to understand.
At this point, the study of "how to use pointers in C language" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.