查找C ++程序的输出(指针)
1.查找以下C++程序的输出。
#include<iostream>
#include<stdlib.h>
using namespace std;
int main(){
float x=5.999;
float *y,*z;
y=&x;
z=y;
cout<<x<<","<<*(&x)<<","<<*y<<","<<*z<<"\n";
return 0;
}输出结果
5.999,5.999,5.999,5.999
说明
原因是x给出了存储在变量x中的值。*(&x)给出存储在地址&x中的数据值,即x的数据值。由于y指向x(..y=&x),因此*y给出x的值。并且由于z与y具有相同的地址,因此*z还给出x的值,即5.999。
2.查找以下C++程序的输出。
#include<iostream>
using namespace std;
int main(){
int track[]={10,20,30,40},*striker;
striker=track;
track[1]+=30;
cout<<"Striker>"<<*striker<<endl;
*striker-=10;
striker++;
cout<<"[email protected]"<<*striker<<endl;
striker+=2;
cout<<"[email protected]"<<*striker<<endl;
cout<<"Reset To"<<track[0]<<endl;
return 0;
}输出结果
Striker>10 [email protected] [email protected] Reset To0
说明
数组磁道包含4个元素{10,20,30,40},指针罢工者持有数组磁道的基地址,即track[0]的地址。
1)*striker保留track[0]的数据值,即10。*striker中的减10将使track[0]=0。
2)递增指针前锋给出轨道的下一个位置,即1。现在,*striker给出track[1]的数据值。
3)再次通过增加2,罢工者到达阵列磁道的4个地址,即磁道[4]。
4)最后在track[0]上打印该值为0(请参阅步骤1)。
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短