指针数组做菜单,指针数组做命令行

来源:互联网 发布:bat运行java程序 编辑:程序博客网 时间:2024/05/21 13:56

一:指针数组做菜单

#define DIM(a) (sizeof(a)/sizeof(*a))//求数组元素个数int main(){int inum=0;int pos=0;int a[10];int i=0;//指针数组 指针数组做函数参数退化为指针char**char* c_keyword[]={"while","case","static","do"};searcheKeyTable(c_keyword,DIM(c_keyword),"do",&pos);//== >带参数的宏searcheKeyTable(c_keyword,(sizeof(a)/sizeof(*a)),"do",&pos);printf("pos:%d\n",pos);searcheKeyTable(c_keyword,DIM(c_keyword),"static",&pos);printf("pos:%d\n",pos);system("pause");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++){//查找key是否在table[]数组中,如果是,返回key的位置if(strcmp(key,table[i])==0){*pos=i;return rv;}}if(i==size){*pos=-1;}return rv;}

二:指针数组做命令行

//操作系统把命令行的操作抓过来后,分配好内存传到main()函数中//指针数组做命令行int main(int argc ,char* argv[],char **cnv){int i=0;printf("*********************** Begin argv ***********************");for(i=0;i<argc;i++){printf("%s\n",argv[i]);}printf("*********************** End argv ***********************");system("pause");return;}



0 0
原创粉丝点击