簡體   English   中英

const int指針錯誤

[英]const int pointer error

我正在學習常量指針,我正在嘗試這個

#include<iostream>
using namespace std ;
int main(){
    int a = 10 ; 
    const int *cp  = &a ;               // my constant pointer variable
    cout<<"\nAddress stored in cp = "<<cp ;
    ++cp;
    cout<<"\nAddress stored in cp = "<<cp   ;
}

它增加了存儲在cp的地址但是根據我到目前為止所理解的,不應該++cp給出錯誤,因為它是一個始終指向同一地址的常量指針,並且該地址不能被修改。

但當我更換

const int *cp = &a ; with int *const cp = &a ;

它給了我這個 在此輸入圖像描述

原諒我的無知但是,他們不是想要同樣的意思嗎?

當你做int *const cp = &a; 它表示一個指向常量cp的整數指針,因此cp無法更改。 但是,在你以前的版本中, const int *cp表示一個指向const int *cp的常量int指針,所以cp指向的值不能改變,但指針本身可以。

通常,人們喜歡從右到左閱讀:

const int *cp cp是指向int常量的指針,因此整數不能改變。

int *const cp = &a; cp是一個指向int的常量指針,因此指針不能改變。

const int *cp  = &a ; 

cp指向的地址內容是只讀的,但指針cp不是

所以,

*cp = value ; //Illegal
++cp ; // Legal

int *const cp = &a ;

指針是只讀的,但是cp指向的地址內容不是

所以,

*cp = value ; //Legal
++cp ; // Illegal

也,

const int *const cp  = &a ;

指針以及cp指向的地址內容都是只讀的

*cp = value ; //Illegal
++cp ; // Illegal

對於從右到左閱讀的簡單聲明

const int *cp1  = &a ; // pointer is variable, pointed to is constant
int *const cp2  = &a ; // pointer is constant, pointed to is variable
const int *const cp3  = &a ; // pointer is constant, pointed to is constant

從而,

cp1++; // possible
*cp1++; // not possible
cp2++; // not possible
*cp2++; // possible
cp3++; // not possible
*cp3++; // not possible

如果它有幫助(並且它可能沒有),則以下是同義詞,利用允許開放類型使const出現在類型的左側右側,但在任何其他限定符之前的語言精確度(像指針或引用):

const int * p; // does NOT require initialization
int const * q; // same as above

兩者都聲明指向常量int數據的指針,並且在語法上是可互換的。

鑒於此:

int * const p = &a; // requires initialization.

聲明一個指向int數據的常量指針; 不是指向常量int數據的指針。

進一步擴展(實際上它們合並 ),我們得到:

const int * const p = &a;
int const * const p = &a;

這些都是同義詞。 兩者都聲明了一個指向常量int數據的常量指針。 指針及其指向的內容都不可修改,並且都需要初始化。


無恥地扯掉圖表

從一個有點相關的問題來看,以下是從我自己(好吧,沒那么多的恥辱)中無恥地扯下來的。 我希望它有助於進一步解釋當你在聲明的不同位置定位const*時會發生什么的差異:

單方向

char *p;               // p is mutable, *p is mutable
const char *p;         // p is mutable, *p is const
char const *p;         // same as above.
char *const p;         // p is const, *p is mutable, must be initialized.
char const *const p;   // p is const, *p is const, must be initialized.

雙間接

char **p;        // ptr-to-ptr-to-char
                 // p, *p, and **p are ALL mutable

const char **p;  // ptr-to-ptr-to-const-char
                 // p and *p are mutable, **p is const

char const **p;  // same as above

char *const *p;  // ptr-to-const-ptr-to-char
                 // p is mutable, *p is const, **p is mutable.

char **const p;  // const-ptr-to-ptr-to-char
                 // p is const, *p is mutable, **p is mutable.
                 // must be initialized.

const char **const p;  // const-ptr-to-ptr-to-const-char
                       // p is const, *p is mutable, **p is const.
                       // must be initialized.

char const **const p;  // same as above

char const *const *p;  // ptr-to-const-ptr-to-const-char
                       // p is mutable, *p is const, **p is const.

const char *const *p;  // same as above.

char *const *const p;  // const-ptr-to-const-ptr-to-char
                       // p is const, *p is const, **p is mutable.
                       // must be initialized.

而我個人的最愛:

char const *const *const p;   // const-ptr-to-const-ptr-to-const-char
                              // everything is const.
                              // must be initialized.

const char *const *const p;   // same as above

這是我總是建議將const放在int的右邊而不是左邊的原因之一。 如:

int const *cp;

並不是:

const int *cp;

這樣做的好處是, const構成的項目是const關鍵字右側的項目, 它的類型位於左側。

在上面,* cp(即cp的內容)是const,它的類型是int。 如果你寫

int * const cp = &foo;

並應用相同的規則, cpconstconst項的類型為int * 當存在多個間接級別時,使用此規則可以更容易地理解發生了什么。

int const ** cpp1;
int * const *cpp2;
int ** const cpp3 = &bar;
int const ** const cpp4 = &xyz;

這最后一個是const指針指向可變的const int 一個const項的類型為int而另一個的類型為int ** 只要你從不在int的左邊放置單詞const就可以理解。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM