What is the difference between const char*, char const*, char*const in C++?
The main content of this article is to explain "what is the difference between const char*, char const*, char*const in C++", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what is the difference between const char*, char const*, char*const in C++?"
Several kinds of introduction to const char*, char const*, char*const are easy to be confused. the following notes introduce the differences between them.
Bjarne gave a mnemonic method in his The C++ Programming Language: read a statement from right to left.
Char * const cp; (* read as pointer to) cp is a const pointer to char const char * p; p is a pointer to const char; char const * p
As above, because there is no const* operator in C++, const can only belong to the previous type.
The C++ standard states that the const keyword is equivalent before the type or variable name.
Const int nexus 5; / / same as belowint const pointer to pointer to const char char 10 * Const int * p; / / same as below const (int) * pint const * Q; / / (int) const * pchar * * p1; / / pointer to pointer to char const char * * p2 * pointer to pointer to char const char * * p3X / pointer to const pointer to char const char * const * p4 * const p5 / / const pointer to pointer to char const char * * const p6 / const pointer to pointer to const char char * const * const p7 / const pointer to const pointer to char const char * const * const p8 / const pointer to const pointer to const char
Speaking of which, we can take a look at an old Google written test:
Const char * p = "hello"; foo (& p); / / function foo (const char * * pp) the following statement is correct
a. The function foo () cannot change the string content that p points to.
b. The function foo () cannot make the pointer p point to the address generated by malloc.
c. The function foo () makes p point to a new string constant.
d. The function foo () assigns p to NULL.
As for the answer to this question, opinions vary. For the above question, we can test it with the following program:
# include#include#includevoid foo (const char * * pp) {/ / * pp=NULL;// * pp= "Hello world!"; * pp= (char *) malloc (10); snprintf (* pp, 10, "hi google!"); / / (* pp) [1] = 'xexamples;} intmain () {const char * p = "hello"; printf ("before foo% sation", p); foo (& p) Printf ("after foo% Spartan", p); p [1] = 'xboy; return;}
The conclusions are as follows:
In the foo function, you can make the new string constant that p points to in the main function.
In the foo function, you can make the p in the main function point to NULL.
In the foo function, you can make the p in the main function point to the block of memory generated by malloc and release it with free in main, but there is a warning. Note, however, that even if p points to a block of memory generated by malloc in foo, you still can't change what p points to with a statement like p [1] ='x'.
In foo, you cannot change the content of p with statements like (* pp) [1] ='x';.
Therefore, it feels that gcc only limits it according to the literal meaning of const, that is, for pointers like const char*p, regardless of whether p actually points to malloc memory or constant memory, you cannot change its contents with statements such as p [1] ='x'. But it's strange that in foo, after pointing to malloc's memory for p, you can modify its contents with functions such as snprintf.
At this point, I believe you have a deeper understanding of "what is the difference between const char*, char const*, char*const in C++". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!