find指定元素指針+指針函數返回

沒有新內容,若有不懂的地方可以看前面關於指針的文章。

#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;
}


加油!!!
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章