指针数组的应用场景之main函数
2015-08-05 16:13
429 查看
#include "stdio.h" #include "string.h" #include "stdlib.h" #include <stdio.h> #include <string.h> //演示:指针数组的用法 //演示:找错误 注意return //求关键字在表中的位置 //一个入口 多个出口 int searcheKeyTable(const char* table[], const int size, const char* key, int *pos) { int rv = 0; int i = 0; int inum = 0; if (table==NULL || key==NULL || pos==NULL) { rv = -1; printf("func searcheKeyTable:%d", rv); return rv; } //间接的证明 数组做函数参数的退回 inum = (sizeof(table)/sizeof(*table)); for(i=0; i<size; i++) { if( strcmp(key, table[i]) == 0 ) { *pos = i; //break; return rv; } } //没有找到返回-1 if (i == size) { *pos = -1; } return rv; } #define DIM(a) (sizeof(a)/sizeof(*a)) int main411() { int inum = 0; int pos = 0; int a[10]; int i = 0; //指针数组 char* c_keyword[] = { "while", "case", "static", "do" }; searcheKeyTable( c_keyword, DIM(c_keyword),"do", &pos); // ===> 带参数的宏 //searcheKeyTable( c_keyword, (sizeof(c_keyword)/sizeof(*c_keyword)),"do", &pos); //searcheKeyTable( c_keyword, inum,"do", &pos); printf("pos:%d\n", pos); //searcheKeyTable(NULL, DIM(c_keyword),"do", &pos); //printf("pos:%d\n", pos); searcheKeyTable( c_keyword, DIM(c_keyword), "static", &pos); printf("pos:%d\n", pos); system("pause"); return ; } //main函数是操作系统调用的函数 //在程序执行的时候可以向main函数传递参数 /* argc 命令行参数 argv 命令行参数数组 env 函数变量数组 int main(); int main(int argc); int main(int argc, char *argv[]) */ int main477(int argc, char* argv[], char**env) { int i = 0; //main02_1(); printf("******************* Begin argv *******************\n"); for(i=0; i<argc; i++) { printf("%s\n", argv[i]); } // for(i=0; argv[i]!=NULL; i++) // { // printf("%s\n", argv[i]); // } printf("******************* End argv *******************\n"); printf("\n"); printf("\n"); printf("\n"); printf("******************* Begin env *******************\n"); for(i=0; env[i]!=NULL; i++) // { printf("%s\n", env[i]); } printf("******************* End env*******************\n"); getchar(); } void main() { int inum = 0; int pos = 0; int a[10]; int i = 0; //指针数组 自我结束能力 char* c_keyword[] = { "while", "case", "static", "do", '\0' }; char* c_keyword2[] = { "while", "case", "static", "do", 0 }; char* c_keyword3[] = { "while", "case", "static", "do", NULL }; for (i=0; c_keyword[i] != NULL; i++) { printf("%s\n", c_keyword[i]); } printf("\n....\n"); for (i=0; c_keyword2[i] != NULL; i++) { printf("%s\n", c_keyword2[i]); } printf("\n....\n"); for (i=0; c_keyword3[i] != NULL; i++) { printf("%s\n", c_keyword3[i]); } system("pause"); } int main45() { int inum = 0; int pos = 0; int a[10]; int i = 0; //指针数组 char* c_keyword[] = { "while", "case", "static", "do", '\0' }; // NULL 0 '\0' for(i=0; c_keyword[i]!=NULL; i++) { printf("%s\n", c_keyword[i]); } system("pause"); }
相关文章推荐
- copy,assign,strong,retain,weak,readonly,nonatomic的区别
- 空气质量在线检测系统制作 之Smart Airbox 制作流程
- armv7s报错: file is universal (4 slices) but does not contain a(n) armv7s slice
- HDU1023 Train Problem 2 (卡特兰数列)
- 南邮 OJ 1389 Mountains of Pandora
- B - And Or---(2015 NEUQ_ACM summer training #1)
- A - Decoding Baby Boos---(2015 NEUQ_ACM summer training #1)
- C - We Love MOE Girls---(2015 summer training #9)
- hdu 1789 Doing Homework again
- (FFOS Gecko & Gaia) OTA - 再进入Gecko层
- 2015 Multi-University Training Contest 5 hdu 5349 MZL's simple problem
- (FFOS Gecko & Gaia) OTA - 重回Gaia层
- Waited long enough for: ServiceRecord 问题解决
- Debug Assertion Failed! Expression: _pFirstBlock == pHead
- postfix与sendmail冲突
- EZ-USB固件框架的追踪研读之main函数3
- saltstack的深入-再次理解state的基本用法
- EZ-USB固件框架的追踪研读之main函数2
- AIX系统修改系统时间
- (FFOS Gecko & Gaia) OTA - 处理check结果