njalic 发表于 2011-4-25 12:17:48

数组c[5]=0;会影响指针变量?

我编制了如下脚本,在调制过程中发现 c【5】=0会影响到指针变量的结果。脚本如下:
Action()
{

int i=0;
char a,b,c;

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=97+i;
        i++;
}
a=0;
lr_output_message("a=%s",a);
//do while循环
i=0;

do {
        b=97+i;
        i++;

} while ( i<5 );

b=0;
lr_output_message("b=%s",b);

for (i=0;i<5;i++) {
        c=97+i;
};


//c=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

njalic 发表于 2011-4-25 13:33:07

弄好了,是数组的问题,把c改成c   或是 a改成a就可以了。

njalic 发表于 2011-4-25 13:33:56

虽然是调出来了,但是不知道原因,希望有高手给解答一下。
页: [1]
查看完整版本: 数组c[5]=0;会影响指针变量?