没有新内容,若有不懂的地方可以看前面关于指针的文章。
#include <stdio.h>
#include <string.h>
int search_number(int *pt, int n, int key)
{
int *p;
for(p = pt; p < (pt + n); p++)
if(*p == key)
return p - pt;
return 0;
}
int *find_address(int *pt, int n, int key)
{
for(int *p = pt; p<pt+n; p++)
if(*p == key)
return p;
return 0;
}
int main()
{
int *test[] =
{
1,2,3,4,5,6,7,8,9,10,20,30,40
};
int *j,key,num;
printf("the element of a array is:\n");
for(int i = 0;i < sizeof(test)/sizeof(test[1]); i++)
{
printf("%d ", *(test+i));
}
printf("\nthe address of test[0] is : %d\n", test);
printf("please input the key number you want to search:");
scanf("%d", &key);
num = search_number(test, sizeof(test)/sizeof(test[0]),key);
printf("\nThe label number of the key number %d in the array is: %d\n", key, num);
j = find_address(test,sizeof(test)/sizeof(test[0]), key);
printf("The point value of the key number %d in the array is : %d", key, j);
return 0;
}
加油!!!

本文介绍了一种使用C语言在数组中查找特定元素的方法,包括返回元素的位置编号和直接找到元素地址的两种函数实现方式。通过示例代码展示了如何创建数组、遍历数组并进行搜索操作。
6258

被折叠的 条评论
为什么被折叠?



