C++ Prime 閱讀筆記之const常量修飾符與指針數組

const char *noerr = "success";  const修飾字符串常量,此時也可寫作char const  *noerr = "success";  意爲不可以通過對*noerr賦值來改變字符常量“success", 但指針noerr本身所指向的地址是可以改變的;而對於const修飾指針的情形只有如下一種形式:

char *const noerr=”success“,意爲noerr指針是不能被修改的,例如noerr++就是有誤的。

二者也可以結合起來使用,如:

const char *const noerr="Success". 意指指向字符常量的常量指針!


關於多維數組和指針也有一個比較難於理解的地方:

     int ia[3][4];      // array of size 3, each element is an array of   ints of size 4 , 要注意[]爲自左向右結合的運算符
     int (*ip)[4] = ia; // ip points to an array of 4 ints 
     ip = &ia[2];       // ia[2] is an array of 4 ints 

由運算符的優先級可知[]比*解引用操作符優先級要高,因而如下兩種表達是有區別的:


int *ip[4]; // array of pointers to int ,四個指針;

int (*ip)[4]; // pointer to an array of 4 ints ;指向數組的指針
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章