标签:blog io ar for div sp c log amp
指针变量指向数组元素:
#import <stdio.h> int main() { int a[10]={1,2,3,4,5,6,7,8,9,0}; int *p; p=a; for (; p<a+10; ) printf("%d\n",*p++); }
数组名做函数参数:
#import <stdio.h> void main() { void anv(int *x,int n); int a[10]={1,2,3,4,5,6,7,8,9},*p; p=a; for (; p<a+9; p++) printf("%3d",*p); printf("\n"); p=a; anv(p,9); for (; p<a+9; p++) printf("%3d",*p); printf("\n"); } void anv(int *x,int n) { int *a,*i,*j,temp,t; a=x; i=a; j=a+n-1; t=(n-1)/2; for (; i<a+t; i++,j--) { temp=*i; *i=*j; *j=temp; } return ; }
选择法对10个整数由大到小排列
#import “stdio.h" void main() { void inv(int *x,int n); int a[10]={2,4,3,5,1,6,6,4,5,8}; int *p; p=a;//(p有确定的值) inv(p, 10); for (;p<a+10;p++) printf("%3d",*p); printf("\n"); } void inv(int *x,int n) { int *i,*k,*j,t; i=x; for (; i<x+n-1; i++) { k=i; for (j=i+1; j<x+n; j++) { if (*j>*k) { k=j; } } if (k!=i) { t=*i;*i=*k;*k=t; } } return ; }
字符串指针做函数参数
#import "stdio.h" void main() { void inv(char *x,char*y); char *a="asdf"; char *b="lkjh"; printf("%s\n%s\n",a,b); inv(a,b); printf("%s\n%s\n",a,b); } void inv(char *x,char *y) { while ((*x++=*y++)!=‘\0‘); }
返回指针值得函数,得到一个返回地址值,用一个指向数组得指针变量存放一个二维数组某行首地址
#import "stdio.h" void main() { float *b(float(* p)[4],int n); float a[][4]={1,2,3,4,5,6,7,8,9,1,2,3}; int m,i; float *c; printf("shuruxuehao:"); scanf("%d",&m); printf("the scores of %d is:\n",m); c=b(a,m); for (i=0; i<4; i++) { printf("%5.2f",*(c+i)); } printf("\n"); } float *b(float(* p)[4],int n) { float *d; d=*(p+n); return d; }
标签:blog io ar for div sp c log amp
原文地址:http://www.cnblogs.com/diyigechengxu/p/3991436.html