c****l 发帖数: 1280 | 1 int main()
{
char *input[] ={"a","b","c",0};
for(int i =0; input[i];i++) {
std::cout << "hello word" << std::endl;
}
return 0;
}
how many "hello word"? why? |
a******p 发帖数: 157 | 2 input 是指针数组,最后一个input[3]=0空指针,其余都指向某个位置
一共3个hello world. |
d****j 发帖数: 293 | 3 这本书上的一个解释:
The C programming Language
By Brian W. Kernighan and Dennis M. Ritchie.
见附件
【在 c****l 的大作中提到】 : int main() : { : char *input[] ={"a","b","c",0}; : for(int i =0; input[i];i++) { : std::cout << "hello word" << std::endl; : } : return 0; : } : how many "hello word"? why?
|
r*******y 发帖数: 1081 | 4 how to explain why I have 6 "hello world" if I change the
code a little bit as below:
int main()
{
char *input[] ={"a","b","c","0"}; // little change here
for(int i =0; input[i];i++) {
std::cout << "hello word" << std::endl;
}
return 0;
}
【在 c****l 的大作中提到】 : int main() : { : char *input[] ={"a","b","c",0}; : for(int i =0; input[i];i++) { : std::cout << "hello word" << std::endl; : } : return 0; : } : how many "hello word"? why?
|
r*******e 发帖数: 7583 | 5 你这是内存越界了,出4个5个6个7个直到任意个都有可能
【在 r*******y 的大作中提到】 : how to explain why I have 6 "hello world" if I change the : code a little bit as below: : int main() : { : char *input[] ={"a","b","c","0"}; // little change here : for(int i =0; input[i];i++) { : std::cout << "hello word" << std::endl; : } : return 0; : }
|
x*********g 发帖数: 69 | 6 答案有吗?
是不是4个hello world,然后出错:input[4]越界了 |
M7 发帖数: 219 | 7 运行一下不就知道了吗?
3 "hello word"
【在 c****l 的大作中提到】 : int main() : { : char *input[] ={"a","b","c",0}; : for(int i =0; input[i];i++) { : std::cout << "hello word" << std::endl; : } : return 0; : } : how many "hello word"? why?
|
x*********g 发帖数: 69 | 8 运行了一下
3个hello world
input[3]=0x00 bad pointer。
问一下,
是不是char *input[]赋值,i位错误时,input[i]默认为0x00 |
r*******y 发帖数: 1081 | 9 thanks.
【在 r*******e 的大作中提到】 : 你这是内存越界了,出4个5个6个7个直到任意个都有可能
|
r*******y 发帖数: 1081 | 10 thanks.
【在 r*******e 的大作中提到】 : 你这是内存越界了,出4个5个6个7个直到任意个都有可能
|
c****l 发帖数: 1280 | 11 the real question is "why". thanks
【在 M7 的大作中提到】 : 运行一下不就知道了吗? : 3 "hello word"
|