上面的行不会编译,因为你没有定义名为''cout''的变量 ,也没有定义名为''的变量ENDL。此外,左移'/ b $ ba指针的值,或指针的值,不是在C中定义的 操作。也许在复制程序中用于发布, 你意外地从另一个窗口粘贴在一行中,你b $ b有一些东西与其他编程语言有关,比如 Rogaine? 当i = 0时,你启动for(j = i - 1; j> = 0; j--)循环, 然后j -will-被初始化为-1,但循环条件j> = 0 将在通过循环进行任何行程之前进行评估,并将 被发现为false,因此循环不会被执行。当j不是至少为0时,j> = 0 保护循环不被执行。 - Man生活只是一种玩笑, 梦想,阴影,泡沫,空气,最好的蒸汽。 - George Walter Thornbury Hi all!This is most certainly a total newbie question, but why doesn''t thefollowing code cause a segfault?void insertion_sort(int a[], int length){int i;for (i=0; i < length; i++){int j, v = a[i];for (j = i - 1; j >= 0; j--){if (a[j] <= v) {cout << a[j] << " <= " << v << endl;break;}a[j + 1] = a[j];}a[j + 1] = v;}}IMHO the segfault should occur at line 9 as on the first pass throughthe first for-loop i = o ergo j is -1 causing a[j] to provoke asegfault.Thanks in advance and excuse me for asking such a dumb question 解决方案This isn''t broken because, first time through, i = 0, so j becomes i - 1 =-1, which is not >= 0, so the loop body is skipped completely.Just for the record, this is a C newsgroup, not a C++ newsgroup.<snip>--Richard Heathfield <http://www.cpax.org.uk>Email: -http://www. +rjh@Google users: <http://www.cpax.org.uk/prg/writings/googly.php>"Usenet is a strange place" - dmr 29 July 1999The second for loop will be entered only when it''s condition is true,i.e., only when j is >= 0. This will not happen after the firstiteration of the outer loop. During the second iteration of the outerloop, j will be initialised to 0 so the code under the inner for loopwill be executed.The above line will not compile, as you have not defined a variablenamed ''cout'', nor a variable named ''endl''. Furthermore, left-shiftinga pointer by a value, or a value by a pointer, is not a definedoperation in C. Perhaps in copying out the program for the posting,you accidently pasted in a line from another window in which youhad something dealing with some other programming language such asRogaine ?When i = 0, and you start the for (j = i - 1; j >= 0; j--) loop,then j -will- be initialized to -1, but the loop condition j >= 0will be evaluated before any trips are taken through the loop, and willbe found to be false, so the loop will not be executed. The j >= 0protects the loop from executing when j is not at least 0.--"Man''s life is but a jest,A dream, a shadow, bubble, air, a vapor at the best."-- George Walter Thornbury 这篇关于新手问题:为什么这样做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 1403页,肝出来的.. 09-06 23:06