-POINTER TO A CONSTANT-
const char *thing_ptr = "Seven";
THEN
thing_ptr = "Forty" /* Legal */
*thing_ptr = 'X' /* Illegal */
BECAUSE
What's thing_ptr? A pointer.
Can it be changed? Of course, it's just a pointer.
What does it point to? A const char array.
Can the data pointed to by thing_ptr be changed? No, it's a constant.
-CONSTANT POINTER-
char *const name_ptr = "Test"
name_ptr = "New"; /* Illegal */
*name_ptr = 'Q'; /* Legal */
What's name_ptr? It's a constant pointer.
Can it be changed? No, it's a constant.
What does it point to? A char.
Can the data being pointed by name_ptr be changed? Yes.
Finally, we can put const in both places.
const char *const title_ptr = "Title".
is a pointer that can't be changed and it points to a data item that can't be changed also.
วันศุกร์, เมษายน 15, 2548
สมัครสมาชิก:
ส่งความคิดเห็น (Atom)
ไม่มีความคิดเห็น:
แสดงความคิดเห็น