码迷,mamicode.com
首页 > 其他好文 > 详细

寻找制定元素的指针

时间:2014-08-21 09:52:43      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:指针   c语言   数组   

<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>

寻找制定元素的指针,布布扣,bubuko.com

寻找制定元素的指针

标签:指针   c语言   数组   

原文地址:http://blog.csdn.net/zxx150633/article/details/38724687

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!