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 use pointers and const modifiers

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

Share

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

This article mainly explains the "pointer and const modifier how to use", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "pointer and const modifier how to use" it!

# # pointers to constants

If you want a pointer to point to a constant, you can declare it by adding const before a pointer variable, that is, the object being pointed to is a constant, so p is a constant and the value of p cannot be changed.

Const int * p; constant pointer

The pointer to the right of the const qualifier on the right of the * sign is itself a const pointer, because the pointer itself is a constant, so the compiler requires that it be given an initialization value, and the pointer must be initialized at the same time as the declaration. That is, the pointer p is constant and cannot point to another address after initialization.

# include int main () {int x = 45; int const sum = 100; int * const p = & x; int * const p2 = ∑ printf ("% d\ n% d\ n", * pjingp2); int y = 55; x = y; printf ("% d\ n", * p); * p = sum; printf ("% d\ n", * p); int * p1 = p Printf ("% d", * p1); return 0;}

# # constant pointer to constant

A pointer to a constant can be declared and then initialized, so you can point the pointer to a constant.

# # pointers to constants to ordinary variables

Although p cannot be negative, the value of the variable can be modified directly to achieve the effect of modifying p.

# include int main () {int x = 256; const int y = 88; const int * p; int * p1; p = & y; printf ("% d\ n", * p); p = & x; printf ("% d\ n", * p); x = 128; printf ("% d\ n", * p); P1 = (int *) & y; printf ("% d\ n", * p1); return 0 } Thank you for your reading, the above is the content of "how to use pointers and const modifiers". After the study of this article, I believe you have a deeper understanding of how pointers and const modifiers are used, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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