指针的入门程序试题
来源:互联网 发布:有道云笔记数据恢复 编辑:程序博客网 时间:2024/06/18 23:08
1、
#include<iostream.h>main(){ //声明变量和指针变量 int a,b,c,*ip; //指针变量ip指向变量a a=100; ip=&a; //使指针变量 ip 指向变量a cout<<"a="<<a<<endl; cout<<"*ip="<<*ip<<endl; cout<<"ip="<<ip<<endl; //指针变量ip指向变量b ip=&b; //使指针变量 ip 指向变量b b=200; cout<<"b="<<b<<endl; cout<<"*ip="<<*ip<<endl; cout<<"ip="<<ip<<endl; //指针变量ip指向变量c ip=&c; //使指针变量 ip 指向变量b *ip=a+b; cout<<"c="<<c<<endl; cout<<"*ip="<<*ip<<endl; cout<<"ip="<<ip<<endl;}
2、
#include<iostream.h>main(){ //声明数组、变量和指针变量 int a[2][3],i,j; int* ip; //从键盘上为数组a赋值 for (i=0;i<2;i++) //为数组a赋值 for (j=0;j<3;j++) { cout<<"a["<<i<<"]["<<j<<"]="; cin>>a[i][j]; } //利用下标变量显示数组a for (i=0;i<2;i++) { for (j=0;j<3;j++) { cout<<a[i][j]<<" "; } cout<<endl; } //利用指针变量显示数组a ip=&a[0][0]; for (i=0;i<2;i++) { for (j=0;j<3;j++) { cout<<"a["<<i<<"]["<<j<<"]="; cout<<ip<<" "; cout<<*ip<<endl; ip++; } }}
3、
#include<iostream.h>main(){ //声明数组、变量和指针变量 int a[]={1,2,3,4,5,6}; int *ip1,*ip2; //测试指针的赋值运算 ip1=a; ip2=ip1; cout<<"*ip1="<<(*ip1)<<endl; cout<<"*ip2="<<(*ip2)<<endl; //测试指针的自增自减运算和组合运算 ip1++; ip2+=4; cout<<"*ip1="<<(*ip1)<<endl; cout<<"*ip2="<<(*ip2)<<endl; //测试指针变量之间的关系运算 int n=ip2>ip1; cout<<"ip2>ip1="<<n<<endl; cout<<"ip2!=NULL="<<(ip2!=NULL)<<endl; //指针变量之间的减法 n=ip2-ip1; cout<<"ip2-ip1="<<n<<endl;}
4、
#include<iostream.h>main(){ // 声明用于存放运动员号码的数组 int h[]={1001,1002,1003,1004}; // 声明用于存放运动员成绩的数组 float x[]={12.3,13.1,11.9,12.1}; //声明用于存放运动姓名的字符型指针数组 char *p[]={"Wang hua","Zhang jian","Li wei","Hua ming"}; //i,j,it是用做循环控制变量和临时变量 int i,j,it; //ft 用做暂存变量 float ft; //pt为字符型指针变量用做暂存指针变量 char *pt; //用选择法对数组x进行排序,并相应调整数组h和p中的数据 for (i=0;i<=3;i++) for (j=i+1;j<=3;j++) if (x[i]>=x[j]) { ft=x[i],x[i]=x[j],x[j]=ft; it=h[i],h[i]=h[j],h[j]=it; pt=p[i],p[i]=p[j],p[j]=pt; } //以下打印排序结果 for (i=0;i<=3;i++) cout<<h[i]<<" ,"<<p[i]<<" ,"<<x[i]<<endl;}
5、
#include<iostream.h>main(){ //声明指针数组 char *colors[]={"Red","Blue","Yellow","Green"}; //指向指针的指针变量 char **pt; //通过指向指针的变量访问其指向的内容 pt=colors; for (int i=0;i<=3;i++) { cout<<"pt="<<pt<<endl; cout<<"*pt="<<*pt<<endl; cout<<"**pt="<<**pt<<endl; pt++; }}
阅读全文
0 0
- 指针的入门程序试题
- 指针的面试题.
- [指针八]有关指针的面试题
- 指针的入门
- 指针的入门
- 指针的经典面试题
- 《C和指针》经典入门程序
- 关于指针,数组指针,函数指针,指针的指针等面试题的详细攻略
- 指针的入门和理解
- 关于指针的入门试验
- 腾讯的面试题(关于指针)
- sizeof和指针地址的两道面试题
- 数组和指针的面试题
- 关于数组指针的一道面试题
- 关于指针的一个面试题
- 传值、引用、传指针的试题
- C/C++的指针经典面试题
- 面试题,简单指针的指向
- java线程池原理讲解及常用创建方式
- HDU
- Reshape the Matrix
- HTML+CSS基础
- Oracle 存储提纲(stored outline)
- 指针的入门程序试题
- android AlarmManager详解,Alarm的设定和取消。
- 关于xampp安装需要注意的问题
- Java日记_17.9.01——点击按钮后,键盘监听失效的原因与解决办法
- CodeForces 112 A.Petya and Strings(水~)
- QT设置环境变量QWS_DISPLAY
- linux内核模块编译
- Spark 自带demo学习日志
- [!] /usr/bin/git clone https://github.com/CocoaPods/Specs.git master --progress Cloning into 'master