第15周项目1-打入“内部”寻“内幕”

来源:互联网 发布:jquery字符串转换数组 编辑:程序博客网 时间:2024/05/23 11:08
/*  *Copyright (c)2014,烟台大学计算机与控制工程学院  *All rights reserved.  *文件名称:C++.cpp  *作    者:王一锟  *完成日期:2014年12月7日  *版 本 号:v1.0  *  *问题描述:  *输入描述:  *输出描述:  */#include<iostream>using namespace std;int main(){    int a=2, b=3, c[5]= {1,2,3,4,5};    int *p1, *p2, *p3;    p1=&a;    p2=&b;    p3=c;    p3++    (*p3)+=10;    cout<<a<<'\t'<<b<<'\t'<<c[1]<<endl;    cout<<*p1<<'\t'<<*p2<<'\t'<<*p3<<endl;    return 0;}


单步执行1:p3=c执行之后





单步执行2:P3++执行之后,地址发生改变


★单步执行3:(*p3)+=10;执行之后c[1]值发生改变



初步判断:p3++  与int i=0  ;c[i++]相似, 地址改变后,*p3=c[1]


程序改进:

#include<iostream>using namespace std;int main(){    int a=2, b=3, c[5]= {1,2,3,4,5};    int *p1, *p2, *p3;    p1=&a;    p2=&b;    p3=c;    p3++;    (*p3)+=10;    cout<<a<<'\t'<<b<<'\t'<<c[1]<<endl;    cout<<*p1<<'\t'<<*p2<<'\t'<<*p3<<endl;    p3++;    (*p3)+=10;    cout<<a<<'\t'<<b<<'\t'<<c[1]<<endl;    cout<<*p1<<'\t'<<*p2<<'\t'<<*p3<<endl;    p3++;    (*p3)+=10;    cout<<a<<'\t'<<b<<'\t'<<c[1]<<endl;    cout<<*p1<<'\t'<<*p2<<'\t'<<*p3<<endl;    return 0;}

单步执行结果:




综上,初步判断成立。


0 0