沒有新內容,若有不懂的地方可以看前面關於指針的文章。
#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;
}
加油!!!