关于间接访问符和自增自减操作符结合使用的问题

名人堂再聚首 2015-09-16 10:06:14
大家好,有这样的一个问题要请教下大家:
在指定的字符串(指针)数组中查找特定的一个字符,原代码如下:

/**
@* Function name:find_char
@* Description:find a specific char in a string array
@* Input:strings: the string array where we search the char
value: the value of the char we need to search
@* Output:0: no matched char is found, 1: we can find the char we need
*/
int
find_char(char **strings, int value) {
assert(strings!=NULL);

/* 对于列表中的每个字符串进行查找 */
while(*strings!=NULL) {
/* 观察字符串中的每个字符,看看它是否是我们查找的那个 */
while(**strings!='\0') {
if (*(*strings)++ == value) {
return 1;
}
}
strings++;
}

return 0;
}

/* 程序用到的实参及调用 */
char *strings[]={"A string", "Another", "Third", "Last", '\0'};
int result=find_char(strings, 'T');


这里主要我不明白的地方是**strings++和*(*strings)++两者之间的区别。
我只知道*(*string)++的解读过程是:
1. 括号内的先计算,*string访问字符串数组中的当前字符串(或指针);
2. 按照C语言中的操作符/运算符优先级,++和*是同级的并按照从右到左的顺序执行,所以先执行++;
3. 后缀++操作符对指针的操作实际是先产生一个指针的拷贝,这里是*string,然后再增加指针的值;
4. 最外层左边的间接访问操作符*实际是访问步骤3中产生的*string指针的拷贝(指针值未增加前的),所以其直接作用是:对当前字符串中的当前字符进行测试,看是否是我们需要的字符,由于++产生的副作用,指向当前字符串字符的指针值增加1。

但如果不对*strings加括号,也就是**strings++这个该如何解读呢?谢谢大家!
...全文
158 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
名人堂再聚首 2015-09-17
  • 打赏
  • 举报
回复
引用 1 楼 fly_dragon_fly 的回复:
后++优先级比*高,前++才跟*一样 strings是char ** 1) **strings++, 先执行++, 因为是后++, 先取地址值, 所以返回的是strings[0][0], 而strings本身+=4 2) *(*strings)++,这次先执行括号, 得到strings[0], 然后执行++, 返回的依然是strings[0][0],但是strings[0]+=1,不改变strings
解释得很经典,非常感谢!
fly_dragon_fly 2015-09-16
  • 打赏
  • 举报
回复
后++优先级比*高,前++才跟*一样 strings是char ** 1) **strings++, 先执行++, 因为是后++, 先取地址值, 所以返回的是strings[0][0], 而strings本身+=4 2) *(*strings)++,这次先执行括号, 得到strings[0], 然后执行++, 返回的依然是strings[0][0],但是strings[0]+=1,不改变strings

69,381

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧