|
我编制了如下脚本,在调制过程中发现 c【5】=0会影响到指针变量的结果。脚本如下:
Action()
{
int i=0;
char a[6],b[5],c[5];
char * bb = "His Excellency the Duke of Exeter";
char *first_a,*last_a;
lr_output_message("the string is :%s ",bb);
while (i<5) {
a[i]=97+i;
i++;
}
a[5]=0;
lr_output_message("a=%s",a);
//do while循环
i=0;
do {
b[i]=97+i;
i++;
} while ( i<5 );
b[5]=0;
lr_output_message("b=%s",b);
for (i=0;i<5;i++) {
c[i]=97+i;
};
//c[5]=0;
//如果不注释了这段,数组c输出的就是abcde??,如果注释了指针bb的值就是变成k。
lr_output_message("c=%s",c);
lr_output_message("the string is :%s ",bb);
first_a=(char *)strchr(bb, 'k');
last_a =(char *)strrchr(bb, 'k');
lr_output_message("the first occurrence of a :%s ",first_a);
lr_output_message("the last occurrence of a :%s ",last_a );
return 0;
}
//注释后的运行结果:
Action.c(10): the string is :His Excellency the Duke of Exeter
Action.c(18): a=abcde
Action.c(29): b=abcde
Action.c(39): the string is :His Excellency the Duke of Exeter
Action.c(45): the first occurrence of a :ke of Exeter
Action.c(46): the last occurrence of a :ke of Exeter
//未注释的运行结果:
Action.c(10): the string is :His Excellency the Duke of Exeter
Action.c(18): a=abcde
Action.c(29): b=abcde
Action.c(36): c=abcde
Action.c(39): the string is :k //这里指针bb的值变成了K,真是出人意料。这是为什么呢?
Action.c(45): the first occurrence of a :k
Action.c(46): the last occurrence of a :k |
|