字符串數組是指針數組,需要使用二級指針
#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; }