本文主要是介绍指针作业题总结(三)SDUT,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
老师上机讲了一个关于字符数组指针的代码。
如下:
#include <stdio.h>
#include <stdlib.h>
//**************************
// function name:
// int comstr( char *str1,char *str2)
// input :
// str1
// str2
// output
// 1 : str1 == str2;
// 0 : str1 != str2;
//********************************
int comstr( char *str1,char *str2)
{while(*str1 !='\0' && *str2 !='\0'){if( *str1 != *str2){return 0;}else{str1++;str2++;}}if(*str1 =='\0' && *str2 =='\0' ){return 1;}
}
char **findstring( char *p1[], char *str)
{int i = 0;int k; //store comstr :outputwhile( p1[i] !=NULL ){k = comstr(*(p1+i), str);if(k==1){return p1+i;}i++;}return NULL;
}int main()
{char *name[]={"LiMing","ZhouDongyu","Liyifeng","Zhaoliying","Yangyang",NULL};int i=0;int num;char searchname[20];char **pch;while( name[i] !=NULL ){// printf("%s\n",name[i]);puts(*(name+i));i++;}pch = name;while(*pch!=NULL){printf("%s\n",*pch);pch++;}printf("\n Find name:\n");fflush(stdin);gets(searchname);pch = findstring(name,searchname);if(pch){printf("\n%s was found:\n",searchname);printf("name[%d]",(pch-name));}else{printf("\n%s Name not found:\n",searchname);}printf("\n Input number:\n");fflush(stdin);scanf("%d",&num);if(num>4){printf("\n Input Error!!!\n");}else{printf("%s\n",name[num]);}return 0;
}
敲完代码的视频总结:
视频讲解
我的总结与思考:
这篇关于指针作业题总结(三)SDUT的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!