寻找制定元素的指针

来源:互联网 发布:剑灵人男捏脸数据图 编辑:程序博客网 时间:2024/06/05 20:19
<span style="font-size:18px;">#include<stdio.h>int search(int *apt,int n,int key)/*返回在数组中和key值相等值的下标*/{int *p;for(p=apt;p<apt+n;p++){if(*p==key)return p-apt;}return NULL;/*不可以在if下面直接else,因为得循环找出和key值相同的值,遍历完了发现没有才返回null*/}int *find(int *apt,int n,int key)/*返回在数组中和key值相等值的地址*/{int *p;for(p=apt;p<apt+n;p++){if(*p==key)return p;}return NULL;}void main(){int a[]={2,3,7,0,5,24,67,45,90,6,1,8,88};int key,i;for(i=0;i<sizeof(a)/sizeof(int);i++)printf("%4d",a[i]);printf("\n");printf("please input the key number!\n");scanf("%d",&key);printf("The address of a[0] is: %d.\n",a);printf("The label number of the key number in the array is %d\n",search(a,sizeof(a)/sizeof(int),key));printf("The adress of the key number %d\n",find(a,sizeof(a)/sizeof(int),key));}</span>

1 1
原创粉丝点击