2010-05-24 50 views
1

可能重複: What is the difference between const int*, const int * const, and int const *?差異各種const聲明的

是什麼下面的區別?

char const *p; 
const char *p; 
char *const p; 

有沒有一個很好的網站,我可以重新學習C和C++?這似乎是我忘了它和求職面試讓我很難...

+0

在線資源無法與[好書]進行比較(http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)。 – 2010-05-24 21:37:20

+3

已經有很多很多重複的東西,例如[const int *,const int * const,int const *之間有什麼區別](http://stackoverflow.com/questions/1143262/what-is-the-difference-between-const-int-const-int- const-int-const) – 2010-05-24 21:40:04

+0

在Posix上,有一個名爲'cdecl'的程序可以使用。 'cdecl explain'const char * p''將回答'聲明p爲指向const char'的指針 – 2010-05-24 21:48:14

回答

17

前兩個是一樣的。

的訣竅是,倒讀吧....

所以第一個是:

 
backwards: p * const char 
read:  p is a pointer to a const char 
meaning: you can change p to point a at something else, but you can't 
      change what it points at 

,最後一個是:

 
backwards: p const * char 
read:  p is a const pointer to a char 
meaning: p is a pointer which you can't change what it points at, but 
      you can change the thing it points to. 
2

前兩個是同樣也意味着一個指向const char的指針。第二個是一個指向(非const)char的常量指針。在第一種情況下,您可以/可以更改指針,而不是指向,指向。在第二種情況下,您可以更改它指向的內容,但不能使指針指向不同的內存。

與指針聲明/定義主要的事情是跟蹤的「*」:

char X * Y ptr; 

無論是「X」或「Y」可以通過const,或volatile,或兩者代替。 'X'替換將修改指針指向AT的內容。 'Y'替換將修改指針本身。在'X'部分,你可以有X charchar X - 這完全沒有區別。與「*」相比,有什麼不同之處在於放置。如果修飾符位於類型的名稱旁邊,它將修改指針指向的內容。如果修飾符位於指針名稱的旁邊,那麼它會修改指針本身。

3

char const *const char *是一樣的。指針是非const的(你可以修改指針),但是字符是const(你不能改變它們指向的字符)。你可以這樣做

p += 1; 

但不是這個

*p += 1; 

char * const是一個const指向非const字符。這意味着你可以做到這一點

*p += 1; 

但不是這個

p += 1; 
0

char const * (char const *):你不能改變通過該指針這個指向值。 Ergo:常量指針。

char *const:您只能初始化某個指針 - 之後無法更改。

此代碼示出它:

int main() 
{ 
    char a = 'a'; 
    char b = 'b'; 

    char const *p1; //p1 is the same as p2 const char = char const 
    const char *p2; 
    char *const p3 = &a; //Can only assign like this. 

    p1 = &a; 
    p2 = &a; 

    *p1 = b; //Will not compile - cannot change the value via the pointer. 
    *p2 = b; //Will not compile - cannot change the value via the pointer. 

    p3 = &a; //Will not compile - cannot reassign the pointer 

    printf("p1 = %c\n",*p1); 
    printf("p2 = %c\n",*p2); 
    printf("p3 = %c\n",*p3); 

    return 0; 
} 
0
  1. 購買的Kernighan & Ritchie(K & R)的副本,並通過它的工作。
  2. 在K & R中進行練習並瞭解The C Answer Book中提供的解決方案。
  3. 購買Accelerated C++的副本並通過它進行工作,因爲它將C++作爲自己的語言,而不僅僅是C語言,並且「位置被閂上」。
  4. 購買Effective C++的副本,請勿違反這些條款並閱讀每條條款,以瞭解爲什麼您不應該違反它們。

作爲額外的獎勵,請閱讀Effective STL正確使用STL

0

這裏有兩個單獨的東西,一個指針數據它指向,並且const可用於任一或兩者。

實施例而不constconst

作爲@tc說

char *p = "hello"; 
++p;    // Changes the pointer, now points to 'e' 
*p = 'u';   // Changes the data, now "hullo" 

實施例中,反向讀取的聲明。

  1. 你可以改變指針,而不是數據它指向

    // These two declarations have the same effect 
    char const *p = "hello"; // Pointer to constant char 
    const char *p = "hello"; // Pointer to char which is constant 
    ++p;    // OK 
    *p = 'u';   // Won't compile 
    

2您可以更改 「你好」,但不是指針

char * const p = "hello"; // Constant pointer to char 
    ++p;    // Won't compile 
    *p = 'u';   // OK 
  • 你可以使兩個不可變

    char const * const p = "hello"; // Constant pointer to constant char 
    ++p;    // Wont' compile 
    *p = 'u';   // Wont' compile