码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-08-15 01:25:18      阅读:98      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!