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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM