Let's take a look at the usage of const-modified pointers through examples:
1,
First, let's look at a common pointer.
Format: int * pTmp
Meaning: a common pointer to the int type
[Cpp]
Int main (int argc, char * argv [])
{
Int I = 1;
Int * pTmp = & I;
Printf ("pTmp = % d \ n", * pTmp );
I = 2;
Printf ("pTmp = % d \ n", * pTmp );
(* PTmp) ++;
Printf ("pTmp = % d \ n", * pTmp );
System ("PAUSE ");
Return 0;
}
Int main (int argc, char * argv [])
{
Int I = 1;
Int * pTmp = & I;
Printf ("pTmp = % d \ n", * pTmp );
I = 2;
Printf ("pTmp = % d \ n", * pTmp );
(* PTmp) ++;
Printf ("pTmp = % d \ n", * pTmp );
System ("PAUSE ");
Return 0;
}
Output:
[Plain]
PTmp = 1
PTmp = 2
PTmp = 3
Press any key to continue...
PTmp = 1
PTmp = 2
PTmp = 3
Press any key to continue...
No problem. You can operate the pTmp pointer as needed.
2,
Format: const int * pTmp
Meaning: The objects pointed to by pTmp are read-only, but pTmp can point to other addresses, that is, pTmp is variable.
[Cpp]
Int main (int argc, char * argv [])
{
Int I = 1;
Int j = 100;
Const int * pTmp = & I;/* correct: indicates that the object pointed to by pTmp is read-only, but pTmp can point to other addresses, that is, pTmp variable */
Printf ("pTmp = % d \ n", * pTmp );
I = 2;
Printf ("pTmp = % d \ n", * pTmp );
PTmp = & j;/* correct: Point pTmp to the address of variable j */
Printf ("pTmp = % d \ n", * pTmp );
(* PTmp) ++;/* error. The object pointed to by pTmp is read-only, and the compilation error prompt is assignment of read-only location */
Printf ("pTmp = % d \ n", * pTmp );
System ("PAUSE ");
Return 0;
}
Int main (int argc, char * argv [])
{
Int I = 1;
Int j = 100;
Const int * pTmp = & I;/* correct: indicates that the object pointed to by pTmp is read-only, but pTmp can point to other addresses, that is, pTmp variable */
Printf ("pTmp = % d \ n", * pTmp );
I = 2;
Printf ("pTmp = % d \ n", * pTmp );
PTmp = & j;/* correct: Point pTmp to the address of variable j */
Printf ("pTmp = % d \ n", * pTmp );
(* PTmp) ++;/* error. The object pointed to by pTmp is read-only, and the compilation error prompt is assignment of read-only location */
Printf ("pTmp = % d \ n", * pTmp );
System ("PAUSE ");
Return 0;
}
3,
Form: int const * pTmp
Meaning: Like 2, it indicates that the object pointed to by pTmp is read-only, but pTmp can point to another address, that is, pTmp is variable.
[Cpp]
Int main (int argc, char * argv [])
{
Int I = 1;
Int j = 100;
Const int * pTmp = & I;/* correct: indicates that the object pointed to by pTmp is read-only, but pTmp can point to other addresses, that is, pTmp variable */
Printf ("pTmp = % d \ n", * pTmp );
I = 2;
Printf ("pTmp = % d \ n", * pTmp );
PTmp = & j;/* correct: Point pTmp to the address of variable j */
Printf ("pTmp = % d \ n", * pTmp );
(* PTmp) ++;/* error. The object pointed to by pTmp is read-only, and the compilation error prompt is assignment of read-only location */
Printf ("pTmp = % d \ n", * pTmp );
System ("PAUSE ");
Return 0;
}
Int main (int argc, char * argv [])
{
Int I = 1;
Int j = 100;
Const int * pTmp = & I;/* correct: indicates that the object pointed to by pTmp is read-only, but pTmp can point to other addresses, that is, pTmp variable */
Printf ("pTmp = % d \ n", * pTmp );
I = 2;
Printf ("pTmp = % d \ n", * pTmp );
PTmp = & j;/* correct: Point pTmp to the address of variable j */
Printf ("pTmp = % d \ n", * pTmp );
(* PTmp) ++;/* error. The object pointed to by pTmp is read-only, and the compilation error prompt is assignment of read-only location */
Printf ("pTmp = % d \ n", * pTmp );
System ("PAUSE ");
Return 0;
}
4,
Format: int * const pTmp
Meaning: pTmp cannot be modified, but the objects pointed to by pTmp can be modified.
[Cpp]
Int main (int argc, char * argv [])
{
Int I = 1;
Int j = 100;
Int * const pTmp = & I;
Printf ("pTmp = % d \ n", * pTmp );
I = 2;
Printf ("pTmp = % d \ n", * pTmp );
PTmp = & j;/* error. pTmp cannot be modified. Compilation prompt: error: assignment of read-only variable 'ptmp '*/
Printf ("pTmp = % d \ n", * pTmp );
(* PTmp) ++;/* correct. The object pointed to by pTmp can be modified */
Printf ("pTmp = % d \ n", * pTmp );
System ("PAUSE ");
Return 0;
}
Int main (int argc, char * argv [])
{
Int I = 1;
Int j = 100;
Int * const pTmp = & I;
Printf ("pTmp = % d \ n", * pTmp );
I = 2;
Printf ("pTmp = % d \ n", * pTmp );
PTmp = & j;/* error. pTmp cannot be modified. Compilation prompt: error: assignment of read-only variable 'ptmp '*/
Printf ("pTmp = % d \ n", * pTmp );
(* PTmp) ++;/* correct. The object pointed to by pTmp can be modified */
Printf ("pTmp = % d \ n", * pTmp );
System ("PAUSE ");
Return 0;
}
5,
Form: const int * const pTmp
Meaning: pTmp cannot be modified, nor can pTmp objects be modified.
[Cpp]
Int main (int argc, char * argv [])
{
Int I = 1;
Int j = 100;
Const int * const pTmp = & I;
Printf ("pTmp = % d \ n", * pTmp );
I = 2;/* correct */
Printf ("pTmp = % d \ n", * pTmp );
PTmp = & j;/* error: assignment of read-only variable 'ptmp '*/
Printf ("pTmp = % d \ n", * pTmp );
(* PTmp) ++;/* error: increment of read-only location */
Printf ("pTmp = % d \ n", * pTmp );
System ("PAUSE ");
Return 0;
}
From Socrates Column