《2022年2022年叫你如何使用c++指针》由会员分享,可在线阅读,更多相关《2022年2022年叫你如何使用c++指针(9页珍藏版)》请在金锄头文库上搜索。
1、? ?o? ?Y?o y? ? y ? ? y ?a? ?t? y ? ? ? 1. #include int main(void) char str=hello world; for(int i=0;i sizeof(str);i+) coutistrn; getchar(); ?1?:hello world /*? ?Dy ?t ?a?a? stri ?a *(str + i) istr ?a *(i + str) 0str+i?a *(0+str+i) */ 2. #include using namespace std; int main() char *str=welcome,to,
2、Fortemedia,Nanjing; char*p=str+1; str0=(*p+)+2; str1=*(p+1); str2=p1+3; str3=p0+(str2-str1); coutstr0endl; coutstr1endl; coutstr2endl; coutstr3endl; system(pause); ?1?: Nanjing jing 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 9 页 - - - - - - - - - g /* str?
3、?y ?e? ?str ? ?a?3char *?char*p=str+1;str? ? D ?char *D ? ?p? ? int ?str+sizeof char* ?p? ?str ? ?t?a? str1? (*p+)+2;?p? ?a int ?p+sizeof char* ? p? ?str2? ?DD*? o?+ ? ? ?p? ?str1? ? ?to ? ? *p D ?char* ? ?(*p+)+2;?str0? ?to? ?2?/0? ?3?str0 ?a? str1=*(p+1);?str1? ?Nanjing? ?coutstr1endl;?3?Nanjing o
4、?coutstr2endl;?3?jing p? ?str2? ? line9?D ?&str2&str2+3?y?YDT?3 ? jing? ? ?coutstr3endl; ?3?g?*/ 3. #include using namespace std; int main() int i2=1073741824,-1073741824; int *p1=&i0; char *p2=(char*)&i0; float *p3=(float*)&i0; cout*p1n*p2n*p3endl; p1+; p2+; p3+; cout*p1n*p2n*p3endl; system(pause);
5、 /* 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 9 页 - - - - - - - - - ? ?D?p1,p2,p3? ? ? ?i0? ? ? ? D2?*? ?DD ?*p1,*p2,*p3?2? p1?* p1? ? ?y?Y ? p1?int*D ? ? ?a&i0?a? ?o-? ? sizeof int ? ?o ?y?Y2?int ? ?a?3 ?intD y?1073741824? ?&i0&i0+3?D? ?0x00?0x00?0x00?0x4
6、0? D? 21? ? ?*p1 ? ?1073741824?p2?char*D ? ? ?a&i0 sizeof char ? ?y?Y ?char ? ?a?3 ?charD y? ?a&i? ?0x00? ?*p2?a0. ? p3?float*D ? ? ?a&i0?a ? ?o-? ? sizeof float? ?o ?y?Y2?float? ?a?3 ?floatD y? floatD ? ?2? ?D ?c/c+? ?y? ?-ieee ? ? ?*p3 ?a2.0 ?2?2? ?a1? float? ?2?2?2? ? a ? ? ? ? D?1 ? ? ? ?+1 ?siz
7、eof ? ? D? */ 4. #include using namespace std; void arrayTest(char str) cout sizeof(str) endl; int main(int argc, char* argv) char str110 = I Love U; arrayTest(str1); system(pause); ?1?:4 /* (1) y ? ?ao yD?2? ?o y? ?¥ ? ?o- ?(2)o ?o?¥ ?o- ? ?1¥ ?3 ?D? ? ? ? 2? ?DT? ? ?y?Y? ?ao yD?2? ?a? ? ?1 ? Y?t?
8、?3? ? D4? ? 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 9 页 - - - - - - - - - */ 1. 1y? o #include void main() int nNumber; int *pPointer; nNumber=15; pPointer=&nNumber; coutnNumber is equal to :nNumberendl; *pPointer=25; coutnNumber is equal to:nNumberendl;
9、 2. ?Y?o y #include void AddFive(int* Number) *Number += 5; void main() int nMyNumber = 18; coutMy original number is nMyNumberendl; AddFive(&nMyNumber); coutMy new number is nMyNumberm_Number = 10; pPointer-m_Character = s; delete pPointer; 4.? y ? int *pArray; pArray = new int6; ?o int *pArray; in
10、t MyArray6; pArray = &MyArray0; / ?o ?DD ? opArray = MyArray; 5. ? y ?a? #include void main() int size; double array = 5.5,4.3,5.2,6.2,7.5; double *p_array; p_array=array; size = sizeof(array)/sizeof(*array); for(int i=0;isize;i+) cout*(p_array+i)endl; cout*(array+i)endl; coutarrayiendl; 6.?t? y ? a
11、ij *(ai+j) *(*(a+i)+j) (*(a+i)j 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 9 页 - - - - - - - - - *(&a00+2*i+j) int a32=3?4?5 ? int *pi; pi=&a00;/pi=a0 /pi=a; ? int a34,(*p)4; p=a; 7. y ? ? ?ey ?3? #include void sum(int *); void main() int a=10,20,30,40,50,N
12、ULL; int i=0; sum(a); while(ai!=NULL) coutai ; i+; void sum(int *p) int total=0; int i=0; while(pi!=NULL) total+=pi; pi=0; i+; couttotalendl; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 6 页,共 9 页 - - - - - - - - - 8.const ? const int *pci; pci=&a; *pci=55;/error!
13、int *const cpi=&a; *cpi=5; cpi=&b;/error! const int *const cpc=&b; cpc=&a;/error! *cpc=44;/error! 9. ? #include void main() char *p=Language; coutp=0;i-) cout*(p+i); coutp; char *p=fortran; coutp=p; #include void main() int len=0; char a10,*p=a; cinp; while(*(p+len)!=0) len+; coutp=pendl; 名师资料总结 - -
14、 -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 7 页,共 9 页 - - - - - - - - - coutlen=lenendl; #include void main() int len=0; char a20,*p=a; cin.get(p,20); while(*(p+len)!=0) len+; coutpendl; coutlenendl; 10. y? int a; int &r=a; #include void swap(int *,int *); void main() int x
15、=5,y=10; coutMain-(1)x=x,y=yendl; swap(&x,&y); coutMain-(2)x=x,y=yendl; void swap(int *px,int *py) int temp; coutSub-(1)x=*px,y=*pyendl; temp=*px; *px=*py; *py=temp; coutSub-x=*px,y=*pyendl; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 8 页,共 9 页 - - - - - - - - - #
16、include int& f(int,int*); void main() int a=1,3,5,7,9; coutf(2,a)endl; int& f(int index,int *a) int &r=aindex; return r; 11. y ? i?a? ? ? ? o &ai,a+i,p+i,π i?a? ? ? o ai,*(a+i),*(p+i),pi; *p+ ? *(p+) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 9 页,共 9 页 - - - - - - - - -