天天看點

給一道關于const的面試題

/*題目:請聲明一個指針,其所指向的記憶體位址不能改變,但記憶體中的值可以被改變
A.const int const *x = &y;
B.int * const x = &y;
C.const int *x = &y;
D.const int * const x = &y;
*/
           
//正确答案B
技巧:
int *x = &y;
()const若在*的左邊,則是x指向的記憶體中的值不可變
()const若在*的右邊,則是x指向的記憶體位址不可變
/////////////////////////////////////////////
依據上述兩條技巧,很容易得出:
A.記憶體中的值不可變,所指向的位址可變
B.記憶體中的值可變,所指向的位址不可變
C.記憶體中的值不可變,所指向的位址可變
D.同上
E.記憶體中的值和所指向的位址都不可變
           

感謝以下部落客文章對此博文的支援:

http://www.cnblogs.com/StudyRush/archive/2010/10/06/1844690.html

http://blog.csdn.net/derkampf/article/details/51149076