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

What are the differences among const char*, char const* and char* const in C++

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces the differences between const char*, char const* and char* const in C++. What is introduced in this article is very detailed and has certain reference value. Interested friends must finish reading it!

1. Const char * ptr

Define a pointer to a character constant. Here, ptr is a constant pointing to type char*, so you cannot use ptr to modify what you point to. In other words, the value of * ptr is const and cannot be modified. But ptr's declaration doesn't mean that the value it points to is actually a constant, it just means that for ptr, the value is constant. The experiment is as follows: ptr points to str, but str is not const. You can modify the value of str directly through the str variable, but not through the ptr pointer.

Gcc compilation error message:

Comment out 16 lines ptr [0] ='s operations; the operation is normal, and the result is:

Hello world

Gello world

In addition, you can modify the value pointed to by the pointer by reassigning the value to the pointer, such as uncommenting lines 7 and 18 in the above code, and the result is:

Hello world

Good game!!

2. Char const * ptr

This method of writing is equivalent to const char *, which can be verified by your own experiments.

3. Char * const ptr

Define a pointer constant to a character, that is, a const pointer. Experiments show that the ptr pointer cannot be modified, but what the pointer points to can be modified. The experiments are as follows:

Gcc error message:

Commenting out 17 lines of code works normally, and the result is:

Hello world

Sello world

For const char* s, const char* is a pointer to a constant, not that the pointer itself is constant and can be uninitialized. The pointer can point to either a constant or a variable, but from the point of view of the pointer, it points to a constant. * s is immutable, s is changeable, and const-defined * s. S is modified by a dereference operator, so s is a normal pointer that can be modified, but the data that s points to (i.e. * s) cannot be modified by pointer s because of the modification of const.

Char * const s declares (* const s), (* const s) is of type char. S is modified by a dereference operator and a const keyword, so s is an immutable pointer, but the data pointed to by s can be modified by pointer s (that is, * s).

Char const * s and const char * s have the same meaning.

If neither s nor the data that s points to is allowed to be modified, then it needs to be declared as const char * const s.

The above is all the contents of this article entitled "what are the differences among const char*, char const* and char* const in C++? thank you for reading!" Hope to share the content to help you, more related knowledge, 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.

Share To

Development

Wechat

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

12
Report