c和指针程序6.2与6.3 分析

这里写图片描述
我觉得内存模型大概是如此:
这里写图片描述
如果改变strings++那么指针将会指向第二行,如果使用(*strings)那么指针将会向后移动一列。
在find_char1函数中:
char *string;
while((string = *strings++)!=NULL){ //将会改变令指针数组指向下一行?
while(*string !=’\0’){
printf(“%c\n”,*string);
if(*string++ == value)
return 1;
}
}
在 find_char2函数中:
while(strings!=NULL){
while(**strings!=’\0’){
printf(“%c\n”,*(*strings));
if(*(*strings)++ == value)//将会改变指针数组指向后面一个数据?
return 1;
}
strings++;

但是最后的结果运行函数一并没有改变?

由于指针数组中每一行类型是数组并没有指针控制,而在每一个指针中却有指针控制。所以*(*strings)便改变了
比如对于定义中的*string[2];
输出第一个数组第二个数组:
printf(“%c\n”,*string[0]+1);

include

include

include “assert.h”

int find_char1(char **,char);
int find_char2(char **,char);
int main(int argc, char *argv[])
{
char *string[2] = {“12”,”34”};
find_char1(string,’1’);
printf(“\t%s\n”,*string);
find_char2(string,’1’);
printf(“\t%s\n”,*string);

system("pause");
return 0;

}

int
find_char1(char **strings ,char value){
char *string;
while((string = *strings++)!=NULL){
while(*string !=’\0’){
printf(“%c\n”,*string);
if(*string++ == value)
return 1;
}
}
return 0;
}

int
find_char2(char **strings,char value){
//assert(strings!=NULL);
while(strings!=NULL){
while(**strings!=’\0’){
printf(“%c\n”,*(*strings));
if(*(*strings)++ == value)
return 1;
}
strings++;
}
return 0;
}

猜你喜欢

转载自blog.csdn.net/zhangkaixu321/article/details/50638935