在gcc中输入一个字符串 [英] input a string in gcc
问题描述
以下是接受来自用户的字符串的代码,但是当我完成输入字符串后我得到
分段错误,即很快我按下了b / b
''输入''键。
following is the code to accept a string from user, but i get
segmentation fault as soon i have finished entering the string i.e. as
soon as i press the ''enter'' key.
推荐答案
文章< e5 * ******** @ f24g2000prh。 googlegroups.com>,
yogicoder< za ***** @ gmail.comwrote:
In article <e5**********************************@f24g2000prh. googlegroups.com>,
yogicoder <za*****@gmail.comwrote:
>以下是代码从用户那里接受一个字符串,但是当我完成输入字符串后我就会出现分段错误,即按下输入键即可。
>following is the code to accept a string from user, but i get
segmentation fault as soon i have finished entering the string i.e. as
soon as i press the ''enter'' key.
> #include< stdio.h>
#include< string.h>
#include<文件stdlib.h>
>#include <stdio.h>
#include <string.h>
#include <stdlib.h>
> int main(void)
{char str [50];
printf("输入字符串:\ n");
fgets(str,sizeof str,stdin);
>int main (void)
{
char str[50];
printf ("Enter string:\n");
fgets (str, sizeof str, stdin);
那部分对我来说没问题。
That part looks okay to me.
//进一步代码
}
//further code
}
你没有从main()返回一个值,导致C89 / C90中未定义的
行为。
但我怀疑错误是在//进一步代码中。部分。
-
Allegories属于思想领域,遗骸是什么样的东西。 - Walter Benjamin
You are not returning a value from main(), which leads to undefined
behaviour in C89/C90.
But I suspect the error is in the "//further code" part.
--
"Allegories are in the realm of thoughts, what ruins are in
the realm of things." -- Walter Benjamin
On Sun,2008年6月8日07:38:48 +0000,Walter Roberson写道:
On Sun, 08 Jun 2008 07:38:48 +0000, Walter Roberson wrote:
文章
< e5 ********************************** @ f24g2000prh。 googlegroups.com>,
yogicoder< za ***** @ gmail.comwrote:
In article
<e5**********************************@f24g2000prh. googlegroups.com>,
yogicoder <za*****@gmail.comwrote:
>>以下是用来接受来自用户的字符串的代码,但是当我完成输入字符串后,即使我按下输入键,我就会收到分段错误。
>>following is the code to accept a string from user, but i get
segmentation fault as soon i have finished entering the string i.e. as
soon as i press the ''enter'' key.
>> #include< stdio.h>
#include< string.h>
#include < stdlib.h中>
>>#include <stdio.h>
#include <string.h>
#include <stdlib.h>
>> int main(void)
{char str [50];
printf( 输入字符串:\ n");
fgets(str,sizeof str,stdin);
>>int main (void)
{
char str[50];
printf ("Enter string:\n");
fgets (str, sizeof str, stdin);
那部分对我来说没问题。
That part looks okay to me.
> //进一步的代码
}
> //further code
}
你没有从main()返回一个值,
You are not returning a value from main(),
如果您将//进一步的代码视为评论,那么您已经知道这个
不是C90程序。如果你把它当作一个指示符来表示有更多的代码,那么该代码很可能会从main()返回一个值。
If you treat //further code as just a comment, then you already know this
is not a C90 program. If you treat it as an indicator that there is more
code, that code may very well return a value from main().
这导致C89 / C90中未定义的
行为。
which leads to undefined
behaviour in C89/C90.
不,它不是。它会导致一个未定义的返回值,但在C90中没有未定义的
行为。
No, it doesn''t. It leads to an undefined return value, but not undefined
behaviour, in C90.
但我怀疑错误是在 //进一步的代码部分。
But I suspect the error is in the "//further code" part.
同意。
Agreed.
yogicoder< za ***** @ gmail.comwrites:
yogicoder <za*****@gmail.comwrites:
以下是接受来自用户的字符串的代码,但是当我完成输入字符串时,我得到
分段错误,即
我按下''enter''键。
following is the code to accept a string from user, but i get
segmentation fault as soon i have finished entering the string i.e. as
soon as i press the ''enter'' key.
错误必须在//其他代码部分中,因为其他人已指出
out。使用-g标志编译代码并使用valgrind或gdb
查看有问题的行。在那之后尝试解决它,如果你不能
(但是先尝试)再次寻求帮助。
-
- Filipe Cabecinhas
char * params [] = {" fil",cab,@,gmail",。 "," com"};
puts(" Real signature:");
for(int i = 0; i< sizeof(params) / sizeof(* params); ++ i)
printf("%s",params [i]);
putchar(''
'');
The error must be in the // further code part, as others have pointed
out. Compile your code with the -g flag and use valgrind or gdb to
see the offending line. After that try to fix it and, if you can''t
(but TRY first) ask for help again.
--
- Filipe Cabecinhas
char* params[] = {"fil", "cab", "@", "gmail", ".", "com"};
puts("Real signature:");
for (int i = 0; i < sizeof(params)/sizeof(*params); ++i)
printf("%s", params[i]);
putchar(''
'');
这篇关于在gcc中输入一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!