第二讲--数组指针和字符串.ppt
文本预览下载声明
*;*;*;*;*;*;*;*;*;void main(void){
char key[ ] = {a,c,b,a,d};
char c[5];
int ques = 0,numques = 5,numcorrect = 0;
cout 输入 numques 个答案:endl;
for(int i=0;inumques;i++) cinc[i];
for(int i=0;inumques;i++) if (c[i]==key[i]) numcorrect++;
string sz1,sz2 ;
sz1.assign(key,5);sz2.assign(c,5);
cout 标准答案: sz1endl;
cout 输入答案: sz2endl;
cout 正确率 = (int)(numcorrect*100.0/numques) %endl;
};运行结果:;存储顺序
按行存放,上例中数组a的存储顺序为:;分行给二维数组赋初值
例如:static int a[3][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12}};
将所有数据写在一个{}内,按顺序赋值
例如:static int a[3][4]={1,2,3,4,5,6,7,8,9,10,11,12};
可以对部分元素赋初值
例如:static int a[3][4]={{1},{0,6},{0,0,11}};;*;*;*;*;*;*;程序运行的结果是:
Output int i=10
Output int pointer i=10
;*;*;pa;;*;*;*;
main(){
int a[10];
int i;
for(i=0; i10; i++)
cina[i];
coutendl;
for(i=0; i10; i++)
couta[i];
};
main( )
{
int a[10];
int i;
for(i=0; i10; i++)
cina[i];
coutendl;
for(i=0; i10; i++)
cout*(a+i);
};使用指针变量;*;*; //输出单位矩阵
coutMatrix test:endl;
for(int i=0;i3;i++) //对指针数组元素循环
{
for(int j=0;j3;j++) //对矩阵每一行循环
{ coutp_line[i][j] ; }
coutendl;
}
};*;在某次运行之后,程序的输出结果为:
0X0065FDE0
11,12,13
0X0065FDEC
21,22,23;*;*;*; pt_int = pig;
*pt_int += dog;
cout Pig now has the value of *pt_int \n;
general = pt_int; //指向int型的指针赋值给void型指针
pt_float = x;
y += 5 * (*pt_float);
cout y now has the value of y \n;
general = pt_float;
//指向float型的指针赋值给void型指针; const char *name1 = John; //指向常量的指针,所指对象之值不能改变
char *const name2 = John; //常量指针,指针本身不能被改变
return 0;
}
运行结果:
Pig now has the value of 34
y now has the value of 38.3125;*;*;*;*;int main( )
{
int index, *point1, *point2;
point1 = index;
*point1 = 77;
point2 = new int;
*point2 = 173;
cout The values are index *point1 *point2 \n;
delete point2;
point1 = new int;
point2 = point1;
*point1 = 999;
cout The values are index *point1 *point2 \n;
显示全部