首页 > 其他 > 详细

第六章 指针6.2 6.3字符串中查找的两个版本

时间:2016-08-15 01:22:30      阅读:247      评论:0      收藏:0      [点我收藏+]
int find_char(char **strings, char ch) 
{
	char *string;
	while ((string = *strings++) != NULL) {
		while (*string != ‘\0‘) {
			if (*string++ == ch) {
				return TRUE;
			}
		}
	}
	return FALSE;
}

无副作用版本,适合多次查找。

int find_char(char **string, char ch) 
{
	while (*string != NULL) {
		while (**string != ‘\0‘) {
              //*string所指向的值被加一 if (*(*string)++ == ch) { return TRUE; } } string++; } return FALSE; }

有副作用版本,*(*string)++会改变*string处的的值,不适合多次查找。第一次查找后会破坏指针数组。

技术分享

如图所示,第二次执行find_char函数时,因为第一次函数运行时,执行了两次*(*string)++找到了字符‘a’,所以*(*string)++,*string对于list[0],自增使list[0]增加两次,list[0]处存放的是指针,所以list[0]向右偏移了两位。从“yangxunwu”,变成“ngxunwu”.

 

第六章 指针6.2 6.3字符串中查找的两个版本

原文:http://www.cnblogs.com/yangxunwu1992/p/5771424.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!