欢迎来到天天文库
浏览记录
ID:39713529
大小:287.00 KB
页数:24页
时间:2019-07-09
《《请登入讨论区网》PPT课件》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、Announcement請登入討論區網頁依照第二次期中考及格與否,有不同作業。Notes請多寫程式,甚至故意把程式寫錯是很重要的Pointer指標,指位器變數有三個屬性:位址、值和資料型態Variablehas3properties:itsaddress,value,anddatatypeinta=5;address0xFFEA5a&apointerPointer指標,指位器(cont.)指標亦為變數,有:值、位址,指標型態Pointerisalsoavariable,has:value,address,andd
2、atatype指標的值為一個位址。Thevalueofapointerisanaddressinta=5;int*b;address&b=0xFE09指向整數變數的指標&a=0xFFEAa=5b=anaddressQuestionaboutpointerCanIassignthevalueofapointerarbitrarily?Ex.int*k;k=0x12;*k=100;Ans:指標的值必須是有意義的1.指標指向其他變數利用指標指向其他變數。Usepointerstostoretheaddressofoth
3、ervariables.inta=5;int*b;address&b=0xFE09指向整數變數的指標&a=0xFFEAa=5b=0xFFEAb=&a;2.指標指向使用者動態分配記憶體mallocDynamicallyallocatememoryforpointersaddress&b=0xFE090xFFEAb=0xFFEAint*b;b=malloc(3*sizeof(int));0xFFEE0xFFF1*b=1;//b[0]=1;*(b+1)=2;//b[1]=2;*(b+2)=3;//b[2]=3;Ques
4、tionWhatiswrongwiththis?int*b;b=malloc(3);//3bytes*b=1;//b[0]=1;*(b+1)=2;//b[1]=2;*(b+2)=3;//b[2]=3;3.指標指向陣列陣列名稱為一個指標常數。Thenameofanarrayvariableisanconstantpointerinta[3];int*b;b=a;*b=1;//b[0]=1;*(b+1)=2;//b[1]=2;*(b+2)=3;//b[2]=3;address&b=0xFE09a=0xFFEAb=0x
5、FFEA0xFFEE0xFFF1a[0]a[1]a[2]QuestionWhatiswrongwiththis?inta[3];a++;Example#include#includevoidmain(void){chars[]="IamanidiotIamanidiot";inti,j;char*ptr;i=0;while(1){ptr=s+i;for(j=0;j<13;j++)printf("%c",*(ptr+j));i++;i=i%14;system("cls");}}
6、4.指標指向二維陣列Pointerisassignedtoan2-darrayinta[2][3];int*b;b=a;*b=1;//a[0][0]=1;*(b+1)=2;//a[0][1]=2;*(b+2)=3;//a[0][2]=3;*(b+3)=4;//a[1][0]=4;address&b=0xFE09a=0xFFEAb=0xFFEA0xFFEE0xFFF1a[0][0]a[0][1]a[0][2]a[1][0]…Twowaystodynamicallyallocate2-darrays(1)char*s
7、;s=(char*)malloc(sizeof(char)*nrow*ncol);for(i=0;i8、poseCcodeWhyusepointers?UsedynamicallyallocatedmemoryHighleveldatastructureEx.Linkedlist(資料結構會教)PassparametersinfunctionsEx.sum#includeintsum(inta,intb);voidmain(void){inta=2,b=3
8、poseCcodeWhyusepointers?UsedynamicallyallocatedmemoryHighleveldatastructureEx.Linkedlist(資料結構會教)PassparametersinfunctionsEx.sum#includeintsum(inta,intb);voidmain(void){inta=2,b=3
此文档下载收益归作者所有