51Testing软件测试论坛

标题: 数组c[5]=0;会影响指针变量? [打印本页]

作者: njalic    时间: 2011-4-25 12:17
标题: 数组c[5]=0;会影响指针变量?
我编制了如下脚本,在调制过程中发现 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
作者: njalic    时间: 2011-4-25 13:33
弄好了,是数组的问题,把c[5]改成c[6]   或是 a[6]改成a[5]就可以了。
作者: njalic    时间: 2011-4-25 13:33
虽然是调出来了,但是不知道原因,希望有高手给解答一下。




欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2