In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the relevant knowledge of what is the difference between pointer constants and constant pointers in C language, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this article on the difference between pointer constants and constant pointers in C language. Let's take a look.
In the interview, we are often asked by the interviewer what is the constant pointer and what is the pointer constant.
The pointer constant is that the pointer itself is constant, and the content (memory address) stored in the pointer is constant and cannot be changed. However, the contents stored in the corresponding memory address can be changed by pointers.
A constant pointer is a pointer to a constant. The corresponding value in the address stored in the pointer is a constant, and its value cannot be modified by the pointer. However, the pointer itself is not a constant and its own value can be changed to point to another address.
Pointer constant and declaration of constant pointer
Declaration of pointer constant: data type * const variable name.
Declaration of constant pointers: data type const * variable name or const data type * variable name.
Constant pointer constant declaration: data type const * const variable name or const data type * const variable name.
Pointer constant and the use of constant pointer
1. Pointer constant example
/ * pointer constant * / / * declarative mode * / int a; int * const b = & a; / / const is placed on the right side of the pointer declaration operator / * example * / int axiom 0memoir 0; int * const pairing chart; pairing & bounding / error * p = 5 / correct
two。 Constant pointer example
/ * constant pointer * / / * declarative method * / 1) const int * pincedram 2) int const * pash / * example * / int axi0coverage0; int const * paddle; pairing & bounding / correct * p = 5True / error
3. Constant pointer constant
/ * constant pointer constant * / / * declaration method * / const int a = 5 position Const int * const pause & a; / / a value remains the same, and the direction of b cannot be changed / * example * / int b = 2 Transint a = 5 Const int * const pointer & a transpose pacifism * error
Example
Int a = 5; const int b = 6; const int * c = & a; / / c is the constant pointer const int * d = & b to the shaping variable a; / d is the constant pointer int * const e = & a to the shaping constant b / / e is the pointer constant to the integer variable a / / int* const f = & b; / the error prompt cannot assign const int* to int* const int* const g = & a; / / g is the pointer constant const int* const h = & b to the integer variable a / / h is the pointer constant printf that points to the shaping constant b ("aGV% d bv% d\ n*c:%d CMV% d\ n*d:%d DV% d\ n*e:%d EV% d\ n*g:%d GRV% d\ n*h:%d GRV% d\ n", aPeragne BMIT cMagnedPersondLigeMagnedLibre MagneDLG MagneDLG MagneDJH); axiom 3 Change the direction of c to b c value change * c change / / * c change; / / error * c is a constant / / change the direction of d to a / d value change * d change / / * d change; / / error * d is constant / / eBay / / the constant pointer to the error cannot be changed. / / at this time, the value of a has changed to 2e and remains the same * e change, that is, a change / / glossary / / the pointer to the error cannot be changed / / * groom1; / / the value in the address where the error is directed is a constant. / / the pointer to the error cannot be changed. / / the pointer to the error cannot be changed. / / the value in the address pointed to by the error message is a constant and cannot change the printf ("after change\ n") Printf ("aRV% d bRV% d\ n*c:%d CRAV% d\ n*d:%d DV% d\ n*e:%d eVV% d\ n*g:%d GRV% d\ n*h:%d HGV% d\ n", aMed brecedence creceddLigeMagneDLINGLING LINGLINGLING LINGRAPH)
The output of the program:
(you can try to comment out what happens when * estrang2 is in your code.)
Summary:
Pointer constant, pointing to the address is immutable, and the value in the address can be changed through the pointer
A constant pointer that points to a value in an address that cannot be changed by a pointer.
Constant pointer constant, the values that point to and point to the address cannot be changed by the pointer
Additional questions
How to write a pointer constant that points to a constant?
Answer:
Const int * const b = & a bank / pointer constants to constants this article ends here on "what's the difference between pointer constants and constant pointers in C language". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "what is the difference between pointer constants and constant pointers in C language". If you want to learn more knowledge, you are welcome to follow the industry information channel.
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.