C: Explanation of pointer with const Modifier

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.