结果:iupJter和Satrun 但是在Bloodshe d开发C ++代码编译很好,但在执行时会生成错误报告。 swap(planet1,0,3)工作正常但是有一些问题 with swap(planet2,3,4) 只有当我为planet2分配空间并执行 snprintf on planet2我得到了预期的结果。 为什么我们在这种情况下需要snprintf ....为什么 planet2 =" Saturn"不工作? PS:如果我错了,请发送你的意见并纠正我...我正在工作 这个问题,为期2天。 What''s wrong with this program? If you were to fix it, what would theintended output be? void swap(char *str, int index1, int index2) {char tmp = str[index1];str[index1] = str[index2];str[index2] = tmp;} int main(int argc, char *argv[]) {char *planet1;char *planet2; planet1 = (char *) malloc(7 * sizeof(char));if (!planet1)return 0; snprintf(planet1, 7, "Jupiter");planet2 = "Saturn"; swap(planet1, 0, 3);swap(planet2, 3, 4); printf("results: %s and %s\n", planet1, planet2);return 0;} My interpretation : In turboc3 compiler(though it is a dead compilernow) this problem is perfectly fine and weget the o/p : results: iupJter and Satrun But in Bloodshed Dev C++ the code compiles fine but onexecution there is an error report generated. swap(planet1,0,3) works fine but there is some problemwith swap(planet2,3,4) Only when i allocate space for planet2 and performsnprintf on planet2 i get the expected result. Why do we need snprintf in this case....why doesplanet2="Saturn" not work? PS:Pls send your comments and correct me if i am wrong...i am workingon this problem for 2 long days. 推荐答案 aj **********@gmail.com 写道: 这个程序有什么问题?如果你要修复它,那么 预期的输出是什么? void swap(char * str,int index1,int index2){ char tmp = str [index1]; str [index1] = str [index2]; str [index2] = tmp; } int main(int argc,char * argv []){ char * planet1; char * planet2; planet1 =(char *)malloc(7 * sizeof(char)); What''s wrong with this program? If you were to fix it, what would theintended output be?void swap(char *str, int index1, int index2) { char tmp = str[index1]; str[index1] = str[index2]; str[index2] = tmp;}int main(int argc, char *argv[]) { char *planet1; char *planet2; planet1 = (char *) malloc(7 * sizeof(char)); " Jupiter"是7个字符,所以你必须分配8.为什么?你搞清楚了...... "Jupiter" is 7 characters, so you must allocate 8. Why? You figure it out... if(!planet1) 返回0; snprintf(planet1,7," Jupiter"); if (!planet1) return 0; snprintf(planet1, 7, "Jupiter"); 使用strcpy()。 Use strcpy(). planet2 =" Saturn" ;; planet2 = "Saturn"; 这里planet2指向一个字符串文字,在大多数情况下是 不可修改。 。 。 Here planet2 points to a string literal, which in most cases isnon-modifiable... > swap(planet1,0,3); swap(planet2,3, 4); > swap(planet1, 0, 3); swap(planet2, 3, 4); 在这里你写给planet2。你发现这不是个好主意。 HTH Bj? [snip] And here you write to planet2. That''s not a good idea, as you discovered.HTHBj?rn[snip] 5月4日,14:28,ajinkya.c ... @ gmail.com写道: On 4 May, 14:28, [email protected] wrote: 什么是错的有这个程序? What''s wrong with this program? 它已经坏了...... It''s broken... 如果你要修理它,会是什么预期输出是? If you were to fix it, what would the intended output be? 我不知道。这不是我的程序。 I don''t know. It''s not my program. void swap(char * str,int index1,int index2){ char tmp = str [index1]; str [index1] = str [index2]; str [index2] = tmp; } void swap(char *str, int index1, int index2) { char tmp = str[index1]; str[index1] = str[index2]; str[index2] = tmp;} int main(int argc,char * argv []){ char * planet1; char * planet2; planet1 =(char *)malloc(7 * sizeof(char)); int main(int argc, char *argv[]) { char *planet1; char *planet2; planet1 = (char *) malloc(7 * sizeof(char)); 我建议你不要施放malloc的结果 - 使用 < stdlib.h-见常见问题 http://c-faq.com/malloc/mallocnocast.html 。 sizeof(char)根据定义,我相信,1,所以它没有用。 你已经分配了7个字符对于planet1。 I would suggest that you don''t cast the result of malloc - use<stdlib.h- see FAQ http://c-faq.com/malloc/mallocnocast.html. sizeof(char) is by definition, I believe, 1, so it served no purpose. You''ve allocated 7 characters for planet1. if(!planet1) 返回0; snprintf(planet1 ,7,木星); if (!planet1) return 0; snprintf(planet1, 7, "Jupiter"); " Jupiter"实际上不是7个字符,是吗? planet1指向的 空间中的结果将是Jupite。 (加上强制性的 尾随''\ 0''; 为什么你觉得需要使用snprintf()而不是strcpy(),或者 strncpy(),我无法想象。 如果你使用snprintf(),你不应该包括< stdio.h> ? "Jupiter" is not actually 7 characters long, is it? The result in thespace pointed to by planet1 will be "Jupite" (plus the obligatorytrailing ''\0''; Why you felt the need to use snprintf() rather than strcpy(), orstrncpy(), I can''t imagine. If you use snprintf(), shouldn''t you include <stdio.h>? planet2 =" Saturn"; planet2 = "Saturn"; planet2指向可能被读取的数据 - 仅... planet2 points to data which may well be read-only... swap(planet1,0,3); swap(planet1, 0, 3); 这将交换 ; J"和i在Jupite中。 This will swap the "J" and "i" in "Jupite". swap(planet2,3,4); swap(planet2, 3, 4); 在我的系统上,正如我所料,当您尝试修改只读内存中的数据时,这会导致分段违反违规。 On my system, as I''d expected, this crashes with segmentationviolation, as you try to modify data in read-only memory. printf(" results:%s and%s\\\",planet1,planet2); 返回0 ; } printf("results: %s and %s\n", planet1, planet2); return 0;} 我的解释:在turboc3编译器中(虽然它是一个死的编译器 现在)这个问题非常好,我们 得到o / p: 结果:iupJter和Satrun My interpretation : In turboc3 compiler(though it is a dead compilernow) this problem is perfectly fine and we get the o/p : results: iupJter and Satrun 真的吗?在这种情况下,turboc3被打破了,因为第一颗行星不能合理地称为木星。 ... Really? turboc3 is broken in that case, as the 1st planet cannotlegitimately be "Jupiter" ... 但是在Bloodshed Dev C ++中,代码编译得很好但是在执行时会生成一个错误报告。 swap(planet1,0,3)工作正常,但有一些问题 与swap(planet2,3,4) 只有当我为planet2分配空间并在planet2上执行 snprintf时,我才能得到预期的结果。 为什么我们在这种情况下需要snprintf .. ..为什么 planet2 =土星不行? But in Bloodshed Dev C++ the code compiles fine but onexecution there is an error report generated. swap(planet1,0,3) works fine but there is some problemwith swap(planet2,3,4) Only when i allocate space for planet2 and performsnprintf on planet2 i get the expected result. Why do we need snprintf in this case....why doesplanet2="Saturn" not work? 请参阅上面关于只读内存的评论。 See my comments above on read-only memory. aj ********** @ gmail.com 写道: 这个程序出了什么问题?如果你要解决它,那么 的预期输出是什么? What''s wrong with this program? If you were to fix it, what would theintended output be? 无论重新设计的程序是写入输出的。 省略 $ b $显然是错误的b #include< stdlib.h> #include< stdio.h> Whatever the redesigned program was written to output. It is obviously wrong to omit#include <stdlib.h>#include <stdio.h> void swap(char * str,int index1 ,int index2){ char tmp = str [index1]; str [index1] = str [index2]; str [index2 ] = tmp; } int main(int argc,char * argv []){ char * planet1; char * planet2; planet1 =(char *)malloc(7 * sizeof(char)); void swap(char *str, int index1, int index2) { char tmp = str[index1]; str[index1] = str[index2]; str[index2] = tmp;}int main(int argc, char *argv[]) { char *planet1; char *planet2; planet1 = (char *) malloc(7 * sizeof(char)); 演员阵容是不必要的和糟糕的编程习惯。 sizeof(char)是定义1. 如果你不想比你的代码更灵活, planet1 = malloc(7); 当然,神奇的数字''7'是个坏主意。 br /> The cast is unnecessay and bad programming practice.sizeof(char) is by definition 1.If you want no more flexibility than your code,planet1 = malloc(7);The magic number ''7'' is a bad idea, of course. if(!planet1) 返回0; snprintf(planet1,7," ; Jupiter); planet2 =土星; if (!planet1) return 0; snprintf(planet1, 7, "Jupiter"); planet2 = "Saturn"; 将以上所有内容替换为 char planet1 [] =" Jupiter"; char planet2 [] =土星; Replace all of the above withchar planet1[] = "Jupiter";char planet2[] = "Saturn"; > swap(planet1,0,3); swap(planet2,3,4 ); > swap(planet1, 0, 3); swap(planet2, 3, 4); 您刚刚尝试修改字符串文字。这是一个非常糟糕的想法 并且具有不可预测的后果。一个可能是一个程序 崩溃。如果你在交换之前用我建议的两行代替 替换main中的所有代码,则不会出现这个问题。 You have just tried to modify a string literal. This is a very bad ideaand has unpredicatable consequences. One possible one is a programcrash. If you replace all of the code in main before the swap with thetwo lines I suggested, this problem will not arise. > ; printf(" results:%s and%s\\\",planet1,planet2); 返回0; } 我的解释:在turboc3编译器中(虽然它现在是一个死的编译器 )这个问题非常好,我们 > printf("results: %s and %s\n", planet1, planet2); return 0;}My interpretation : In turboc3 compiler(though it is a dead compilernow) this problem is perfectly fine and we TurboC让你逃脱这只是运气不好。如果你的程序崩溃了,那么对你来说会更好。 That TurboC allowed you to get away with this is just bad luck. Itwould have been better for you if your program has crashed. 得到o / p: 结果:iupJter和Satrun 但是在Bloodshed Dev C ++中,代码编译得很好但是在执行时会生成错误报告。 get the o/p : results: iupJter and Satrun But in Bloodshed Dev C++ the code compiles fine but onexecution there is an error report generated. 因为你愚蠢地尝试修改字符串文字。 Because you foolishly try to modify a string literal. > swap(planet1,0,3)工作正常,但有一些问题 with swap(planet2,3,4) > swap(planet1,0,3) works fine but there is some problemwith swap(planet2,3,4) 不开玩笑。 No kidding. 仅当我为planet2分配空间时并且在planet2上执行 snprintf我得到了预期的结果。 为什么我们在这种情况下需要snprintf ....为什么 planet2 =土星不行? Only when i allocate space for planet2 and performsnprintf on planet2 i get the expected result. Why do we need snprintf in this case....why doesplanet2="Saturn" not work? 你不需要snprintf()。除了我建议的替换 之外,还可以使用strcpy,memcpy,memmove和sprintf中的任何一个。 还有其他选择。 planet2 =土星; *确实*有效。它将planet2指向字符串文字。你试图修改失败的字符串文字。 You don''t need snprintf(). In addition to my suggested replacementabove, any of strcpy, memcpy, memmove, and sprintf could have been used.There are other choices as well.planet2 = "Saturn";*does* work. It points planet2 to a string literal. It is you attemptto modify the string literal that fails. > PS:请发送如果我错了,你的意见和纠正我...我正在工作 这个问题,为期2天。 >PS:Pls send your comments and correct me if i am wrong...i am workingon this problem for 2 long days. 您可以在几分钟内查看有关 指针与数组之间差异的常见问题解答文章,甚至(恐怖)阅读您的教科书, 防止浪费2天。 You could have just checked the FAQ articles on the difference betweenpointers and arrays in minutes, or even (horrors) read your textbook,preventing the waste of 2 long days. 这篇关于查询指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-19 20:57