c语言字符串数组的查找

                                                                        字符串数组是指针数组,需要使用二级指针

                    

#include "stdafx.h"
#include <stdio.h>
#include <string.h>



const char* str[] = { "Hello","abc","applef","man","C程序设计","指针数组" };
const char* pdest = "指针数组";


static int str_search(const char*key, const char**pstr, int num)
{
    int i;

    for (i = 0; i < num; i++)
    {
        //// 指针数组  p首先是个指针  然后指向类型是地址 所以是二级指针
        if (strcmp(*pstr++, key) == 0)
        {
            return 0;
        }

    }

    return -1;

}


int main()
{

    int ret;
    ret = str_search(pdest, str, sizeof(str) / sizeof(char*));
    if (ret == 0)
    {
        printf("查找成功\n");
    }
    else
    {

    }

    printf("\n");        
    printf("i = %d\n", sizeof(str) / sizeof(char*));

    while (1);

    return 0;
}

 

猜你喜欢

转载自www.cnblogs.com/nowroot/p/12642411.html