const char*和char *const

1.const char*

const in front of the pointer: Modification of the constant --The pointer to the constant pointer can be changed, but the value pointed to by the pointer cannot be changed

char ch[5] = "list";
	const char* pStr = ch;
	//pStr = 'w';        error
	pStr = "hi";

Tip : Although pStr cannot be used to modify the value of the array element it points to, we can modify the value of the element through the array variable ch.

Application scenario : When defining a function, use the pointer type to pass parameters, usually the formal parameter is declared as const char* type, so that the content pointed to by the pointer cannot be modified by the formal parameter, so as to ensure the consistency of the data

2.char *const

const after the pointer: modify the pointer --- pointer constant pointer point can not be changed, the value pointed to by the pointer can be changed

	char ch2[5] = "list";
	char* const pStr2 = ch2;
    std::cout << "Hello World!\n";

summary:

The const char type pointer (pointer to constant) points to a constant and cannot be modified, but its pointer value can be modified. But for a pointer of type char*const (constant pointer), its address is a constant, that is, its pointer value is a constant and cannot be modified, but the content it points to can be modified. *

Guess you like

Origin blog.csdn.net/weixin_50188452/article/details/114684904