Get the App
SLTechnology News&Howtos  ›  Servers  › 

C language pointer to constant and constant pointer

Shulou Source: shulou.com Published: 2022-06-03 04:11:37 09月30日 Update

A pointer to a constant: the value of the pointer cannot be changed, and the pointer is not a constant, so the pointer can be modified.

Int main (void) {int value = 10; / * define a pointer to a constant * / const int * pValue = & value; / / pValue = & value; / * * the compiler will report an error and * pValue is read-only. * that is, the value pointed to by the pointer cannot be changed * / / * * the assignment compiler will report an error assignment of read-only location'* pValue' * because the value pointed to by * pValue is constant, so it cannot be changed. * / * pValue = 20; / * but you can change the value of value * / int number = 30; / * the pointer is not constant, so you can change the pointer to * / pValue = & number; return 0;}

Constant pointer

# include

/ * *

Constant pointer

The address stored in the pointer cannot be changed

/

Int main (void)

{

Int value = 10

Int const pValue = & value

Int item = 34

/ * *

Compiler error, assignment of read-only variable 'pValue'. Indicates that the address stored in the pointer cannot be modified

/

PValue = & item

/ but you can modify the value pointed to by the address /

PValue = 24

Return 0

}

Tags: Pointer point constant address compiler compile report store that is language Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB MySQL Microsoft macOS OPPO Reno