2013年3月16日 星期六

Pointer 2_const

(1)a non-constant pointer to non-constant data

存取權最高
Data可透過pointer遭到更改
Pointer可以改為只向別的物件
Ex: pointer3.c
----------------------------------------------------------------------------------

/* Converting lowercase letters to uppercase letters
using a non-constant pointer to a non-constant data */
#include <stdio.h>
#include <ctype.h>
void convertToUppercase(char *ptr);
int main(void)
{
char string[]="characters and $32.25";
printf("The string before conversion is: %s",string);
convertToUppercase(string);
printf("\nThe string after conversion is:%s\n",string);
return 0;
}
void convertToUppercase(char *ptr)
{
while(*ptr !='\0')
{
if(islower(*ptr))
{
*ptr=toupper(*ptr);
}
++ptr;
}
}
-------------------------------------------------------------------------------------------
(2)a non-constant pointer to constant data

Ex: pointer5.c
若隨意更改指標指向的物件內容會有error
編譯錯誤: assignment of read only location “ptr
明顯的問題出在ptr指向的是read only的位置,不能更改
Tips: 陣列當引數呼叫function時,陣列會自動以傳參考呼叫function,struct則是永遠以傳值的方式,傳遞整個副本過去會造成執行的負擔,因此必須將struct傳給函式時使用指向constant data的指標,一方面獲得傳參考呼叫的效率,一方面能保護資料。也就是說只要複製一份struct本身的位址就好。--一個時間/空間取捨(time/space trade-off)的例子
Important: memory 不夠大且執行效率很重要時,用指標
memory 很大且效率不那麼重要,用call by value實行最小權限原則
-----------------------------------------------------

/* Attempting to modify data through a non-constant pointer to constant data */
#include<stdio.h>
void f(const int *ptr);

int main (void)
{
int y;
f(&y);
return 0;
}
/* ptr cannot be used to modify the value of variable to which it points */
void f(const int *ptr)
{
*ptr=100; //error: cannot modify a const object
}

-----------------------------------------------------------------------------------------------
(3)a constant pointer to a non- constant data

非常數指標的常數指標永遠會指向同一個記憶體位置,不過指標所指向的數值可以更改。
Array名稱預設就是這種指標。
Const的指標必須在宣告時指定初始值
若指標是函式的參數,則初始值將設成傳入此函式的指標
Ex:pointer6.c
Ptr是一個指向整數的常數指標,但後來又指向y,此時編譯器會error—assignment of read only variable “ptr
-------------------------------------------------------------------------------------------------------------------

/* Attempting to modify a constant pointer  to a non-constant data */
#inlcude <stdio.h>
int main(void)
{
int x;
int y;
/* ptr is a constant pointer to an integer that can be modified through ptr,
but ptr always points to the same memory lacation */
int *const ptr=&x;
*ptr=7;
ptr=&y;

return 0;
}

-------------------------------------------------------------------------------------------------------------------
(4)a constant pointer to a constant data

存取權最低
Pointer永遠指向同一個記憶體位置,且該位置內容不能更改
Ex: pointer7.c
  Const int *const  ptr=&x;
  Ptr是一個常數指標,指向一個整數常數
  程式中藥更改指標與指標指向的內容都失敗 Assignment of read only  location “ptr”  Assignment of read only  variable “ptr
-----------------------------------------------------------------------------------------------------------------

/* Attempting to modify a constant pointer to constant data */
#include<stdio.h>
int main(void)
{
int x=5;
int y;

/*ptr is a constant pointer to a constant integer.ptr always
points to the same location;the integer at that location
cannot be modified */
const int *const ptr=&x;
printf("%d\n",*ptr);
*ptr=7; //error : *ptr is const;cannot assign new value 
ptr=&y; //error:  ptr is const;cannot assign new address
return 0;
}

-----------------------------------------------------------------------------------------------------------------



沒有留言:

張貼留言

全國推廣動物認領養平台串聯貼紙

全國推廣動物認領養平台串聯貼紙