一道FOR循环的问题(初级)
#include <string.h>
#include <iostream.h>
void main()
{
unsigned int i;
char a[]= "hello ";
char *pa=a;
cout < <a < <endl;
for(i=o;i <strlen(a);i++)
cout < <pa[i];
cout < <endl;
for(i=0;i <strlen(a);i++)
cout < <(pa+i) < <endl;
}
为什么结果会是:
hello
hello
hello
ello
llo
lo
o
请附上程序流程
[解决办法]
void main()
{
unsigned int i;
char a[]= "hello ";
char *pa=a;
cout < <a < <endl; /* 输出第一个hello */
for(i=o;i <strlen(a);i++) /* 开始输出第二个hello,循环输出字符串数组的每个字符 */
cout < <pa[i]; /* 每次输出一个字符,依次为 'h ' 'e ' 'l ' 'l ' 'o ' */
cout < <endl; /* 输出换行符 */
for(i=0;i <strlen(a);i++) /* 开始输出第三个hello和后面的字符串 */
cout < <(pa+i) < <endl; /* 循环中每次的pa+i为一个字符串*/
/*从原始字符串的第i个字符开始的字符串 */
}
[解决办法]
楼主加上点儿东西,就可以看得很清楚了:
void main()
{
unsigned int i;
char a[]= "hello ";
char *pa=a;
cout < < "----------------1--------------- " < < endl;
cout < <a < <endl;
cout < < "-----------------2-------------- " < < endl;
for(i=o;i <strlen(a);i++)
cout < <pa[i];
cout < <endl;
cout < < "---------------3---------------- " < < endl;
for(i=0;i <strlen(a);i++)
cout < <(pa+i) < <endl;
cout < < "----------------4--------------- " < < endl;
}
这样分隔开之后,你就可以看清楚那部分打印出什么东西了。