什麼叫指針?
指針某一變量或函數的記憶體位址,是一個無符号整數,它是以系統尋址範圍為取值範圍,32位,4位元組。
指針變量:
存放位址的變量,在C++中,指針變量隻有有了明确的指向才有意義。
指針類型
int*ptr; //指向int類型的指針變量
char*ptr;
float*ptr;
指針的指針:
char*a[]={"hello","the","world"};
char**p=a;
p++;
cout<<*p<<endl; //輸出the
函數指針:
指向某一函數的指針,可以通過調用該指針來調用函數。
例子:
int max(int ,int);
int (*f)(int int)=&max;
d=(*f((*f)(a,b),c));
指針數組:
指向某一種類型的一組指針(每個數組變量裡面存放的是位址)
int*ptr[10];
數組指針:
指向某一類型數組的一個指針
int v[2][10]={{1,2,3,4,5,6,7,8,9,10},{11,12,13,14,15,16,17,18,19,20}};
int (*a)[10]=v;//數組指針
cout<<**a<<endl; //輸出1
cout<<**(a+1)<<endl; //輸出11
cout<<*(*a+1)<<endl; //輸出2
cout<<*(a[0]+1)<<endl; //輸出2
cout<<*(a[1]+1)<<endl; //輸出12
cout<<a[0]<<endl; //輸出v[0]首位址
cout<<a[1]<<endl; //輸出v[1]首位址
int*p與(int*)p的差別
int*p:p指向×××的指針變量
(int*)p:将p類型強制轉換為×××的指針
數組名相當于指針,&數組名相當于雙指針
int a[]={{1,2,3,4,5};
int*ptr=(int*)(&a+1);//二維數組,整體加一行
printf("%d%d",*(a+1),*(ptr-1));//輸出25
char*str="helloworld"與char str[]="helloworld"的差別
char*str="helloworld":配置設定全局數組,共享存儲區
char str[]="helloworld":配置設定局部數組