赞
踩
第1关 通过指针变量间接访问变量
- #include <stdio.h>
- int main()
- {
- int x,y;
- int *p; //定义指针变量p
- p=&x; //使指针变量p指向一个变量x
- /***** 请在以下填写通过p输入输出x值代码 *****/
- /********** Begin **********/
- scanf("%d",p);
- printf("%d\n",*p);
- /********** End **********/
- p=&y; //使指针变量p指向另一个变量y
- /***** 请在以下填写通过p输入输出y值代码 *****/
- /********** Begin **********/
- scanf("%d",p);
- printf("%d",*p);
-
- /********** End **********/
- return 0;
- }
第2关 交换两个指针变量的值
-
- #include <stdio.h>
- int main()
- {
- int a=100,b=200;
- int *p1, *p2, *p;
- p1=&a;
- p2=&b;
- scanf("%d%d",p1,p2);
- /***** 交换p1、p2的值 *****/
- /********** Begin **********/
- p=p1;p1=p2;p2=p;
-
- /********** End **********/
- printf("%d %d\n",*p1,*p2);
- return 0;
- }
-
-
- #include<stdio.h>
- #define M 3
- main()
- {
- int a[M],i;
- int *max,*min;
- for(i=0 ;i<M; i++)
- {
- scanf("%d",&a[i]);
- }
- /***** 将指针变量max,min初始化 *****/
- /********** Begin **********/
- max=&a[0],min=&a[0];
-
- /********** End **********/
- /***** 将每个数组元素依次与max,min指向的数组元素比较,使max,min分别指向的最大、最小的数组元素 *****/
- /********** Begin **********/
- for(i=0;i<M;i++)
- {
- if(a[i]>*max)
- max=&a[i];
- else if(a[i]<*min)
- min=&a[i];
- }
-
- /********** End **********/
- /***** 通过指针变量max,min输出最大值和最小值 *****/
- /********** Begin **********/
- printf("min=%d\nmax=%d",*min,*max);
-
- /********** End **********/
- return 0;
- }
第4关 通过指针变量存取一维数组元素
- #include <stdio.h>
- int main()
- {
- int a[10],*p;
- /*****输入10个整数存入数组a *****/
- for( p=a;p<a+10;p++ ) // p指向a[0]
- {
- scanf("%d",p);
- }
-
- /*****输出数组a 的所有数组元素的值*****/
- /********** Begin *********/
- for(p=a;p<a+10;p++)
- {
- printf("%d ",*p);
- }
- /********** End **********/
- printf("\n");
- return 0;
- }
第5关 用指针实现数组元素逆置
- #include <stdio.h>
- #define N 10
- int main()
- {
- /********** Begin **********/
- int a[N],*i,*j,t,*p=a,k;
- for(;p<a+N;p++)
- {
- scanf("%d",p);
- }
- for(i=&a[0],j=&a[N-1];i<j;i++,j--)
- {t=*i;*i=*j;*j=t;}
- for(p=a;p<a+N;p++)
- {
- printf("%d ",*p);
- }
- /********** End **********/
- return 0;
- }
第6关 不同数据类型所占内存的字节数
- #include <stdio.h>
- int main ()
- {
- char a2, b2[5],c2[10],d2[3][4],e2[2][5];
- double a3, b3[5],c3[10],d3[3][4],e3[2][5];
- float a4, b4[5],c4[10],d4[3][4],e4[2][5];
- /*****输出a2,b2, b2[0],c2,d2,d2[0], d2[0][0],e2,e2[0],e2[0][0]所占内存的字节数*****/
- /********** Begin *********/
- printf("a2 = %d\n",sizeof(a2) );
- printf("b2 = %d\n",sizeof(b2) );
- printf("b2[0] = %d\n",sizeof(b2[0]));
- printf("c2 = %d\n",sizeof(c2));
- printf("d2 = %d\n",sizeof(d2));
- printf("d2[0] = %d\n",sizeof(d2[0]));
- printf("d2[0][0] = %d\n",sizeof(d2[0][0]));
- printf("e2 = %d\n",sizeof(e2));
- printf("e2[0] = %d\n",sizeof(e2[0]));
- printf("e2[0][0] = %d\n",sizeof(e2[0][0]));
- /********** End **********/
-
- /*****输出a3,b3, b3[0],c3,d3,d3[0],d3[0][0],e3,e3[0],e3[0][0]所占内存的字节数*****/
- /********** Begin *********/
- printf("a3 = %d\n",sizeof(a3));
- printf("b3 = %d\n",sizeof(b3));
- printf("b3[0] = %d\n",sizeof(b3[0]));
- printf("c3 = %d\n",sizeof(c3));
- printf("d3 = %d\n",sizeof(d3));
- printf("d3[0] = %d\n",sizeof(d3[0]));
- printf("d3[0][0] = %d\n",sizeof(d3[0][0]));
- printf("e3 = %d\n",sizeof(e3));
- printf("e3[0] = %d\n",sizeof(e3[0]));
- printf("e3[0][0] = %d\n",sizeof(e3[0][0]));
- /********** End **********/
-
- /*****输出a4,b4, b4[0],c4,d4,d4[0], d4[0][0],e4,e4[0],e4[0][0]所占内存的字节数*****/
- /********** Begin *********/
- printf("a4 = %d\n",sizeof(a4));
- printf("b4 = %d\n",sizeof(b4));
- printf("b4[0] = %d\n",sizeof(b4[0]));
- printf("c4 = %d\n",sizeof(c4));
- printf("d4 = %d\n",sizeof(d4));
- printf("d4[0] = %d\n",sizeof(d4[0]));
- printf("d4[0][0] = %d\n",sizeof(d4[0][0]));
- printf("e4 = %d\n",sizeof(e4));
- printf("e4[0] = %d\n",sizeof(e4[0]));
- printf("e4[0][0] = %d\n",sizeof(e4[0][0]));
- /********** End **********/
- return 0;
- }
第7关 用指向数组的指针遍历二维数组
- #include <stdio.h>
- int main()
- {
- int a[3][4],i,j;
- int(*p)[4]; //定义p为指针,指向一个长度为4的整型数组
- p = a;
- /********** Begin *********/
- for(i=0;i<3;i++)
- {
- for(j=0;j<4;j++)
- {
- scanf("%d",(*(a+i)+j));
- }
- }
- for(i=0;i<3;i++)
- {
- for(j=0;j<4;j++)
- {
- printf("%d ",a[i][j]);
- }printf("\n");
- }
- /********** End **********/
- return 0;
- }
- #include <stdio.h>
- int main ()
- {
- char * a2,**b2,*c2[4], (*d2)[5];
- double * a3,**b3,*c3[4], (*d3)[5];
- float * a4,**b4,*c4[4], (*d4)[5];
- /*****输出a2,b2,c2,d2所占内存的字节数*****/
- /********** Begin *********/
- printf("a2 = %d \n",sizeof(a2));
- printf("b2 = %d \n",sizeof(b2));
- printf("c2 = %d \n",sizeof(c2));
- printf("d2= %d \n",sizeof(d2));
- /********** End **********/
- /*****输出a3,b3,c3,d3所占内存的字节数*****/
- /********** Begin *********/
- printf("a3 = %d \n",sizeof(a3));
- printf("b3 = %d \n",sizeof(b3));
- printf("c3 = %d \n",sizeof(c3));
- printf("d3 = %d \n",sizeof(d3));
- /********** End **********/
- /*****输出a4,b4,c4,d4所占内存的字节数*****/
- /********** Begin *********/
- printf("a4 = %d \n",sizeof(a4));
- printf("b4 = %d \n",sizeof(b4));
- printf("c4 = %d \n",sizeof(c4));
- printf("d4 = %d \n",sizeof(d4));
- /********** End **********/
- return 0;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。