读取用户输入时出现 fgets 分段错误
fgets segmentation fault when reading input from user
这是使用 ubuntu
的违规代码
char *name;
int main(void)
{
fgets(name, sizeof(name), stdin);
}
void HUD()
{
printf("%s ", name);
}
这是我的问题。我从 scanf("%s", &name) 开始,并在字符串末尾变得垃圾。在过去的 2 个小时里,我一直在阅读有关 scanf 和 fgets 的文档,因为显然当您不知道所需数组的大小时不应使用 scanf,(并且由于用户输入的大小可能会有所不同)我决定尝试使用 fgets。我也尝试通过 char name[100]; 设置一个固定值。并通过 fgets(name, 100, stdin)
现在我遇到了分段错误,通过阅读我在 google 的前两页找到的每个结果,我的语法看起来是正确的,但我在 cboard 或这里没有找到任何可以修复的东西我的问题。
有什么想法吗?
char *fgets(char *restrict s, int n, FILE *restrict stream);
The fgets() function shall read bytes from stream into the array
pointed to by s, until n-1 bytes are read, or a is read and
transferred to s, or an end-of-file condition is encountered. The
string is then terminated with a null byte. [0]
您需要将其分配到特定大小并使用该大小调用 fgets。此代码可以帮助您完成同样的事情,但它有一个固定大小的缓冲区。
#include <stdlib.h>
#include <stdio.h>
char name;
char* buffer;
int buffer_size = 16;
int i = 0;
void HUD()
{
printf("%s ", buffer);
}
int main(void)
{
buffer = malloc(buffer_size);
if(!buffer) return;
for(;;) {
name = getchar();
if(name < 0) {
buffer[i] = '[=10=]';
goto finish;
} else if(i < (buffer_size -1)) {
buffer[i++] = name;
} else if(name == '\n') {
break;
}
}
buffer[i] = '[=10=]';
finish:
HUD();
free(buffer);
return 0;
}
[0] http://pubs.opengroup.org/onlinepubs/009695399/functions/fgets.html
sizeof(name)
将是您系统上指针的大小,在我的系统上是 8 个字节。不是缓冲区的大小,正如您所期望的那样
另外 char* name
未初始化。您将尝试写入未初始化的缓冲区,它将以未定义的行为结束。
要解决,要么将其设为固定大小的缓冲区,要么在堆上分配一些 space。
分配
#include <stdio.h>
#include <stdlib.h>
#define NAME_SIZE 100
char *name;
void HUD()
{
printf("%s ", name);
}
int main(void)
{
name=calloc(NAME_SIZE, sizeof(char));
fgets(name, NAME_SIZE, stdin);
HUD();
free(name);
}
静态数组
#include <stdio.h>
#include <stdlib.h>
#define NAME_SIZE 100
char name[NAME_SIZE];
void HUD()
{
printf("%s ", name);
}
int main(void)
{
fgets(name, NAME_SIZE, stdin);
HUD();
}
您必须将缓冲区的大小传递给 fgets
,以便它知道必须写入多少 space。
这是使用 ubuntu
的违规代码char *name;
int main(void)
{
fgets(name, sizeof(name), stdin);
}
void HUD()
{
printf("%s ", name);
}
这是我的问题。我从 scanf("%s", &name) 开始,并在字符串末尾变得垃圾。在过去的 2 个小时里,我一直在阅读有关 scanf 和 fgets 的文档,因为显然当您不知道所需数组的大小时不应使用 scanf,(并且由于用户输入的大小可能会有所不同)我决定尝试使用 fgets。我也尝试通过 char name[100]; 设置一个固定值。并通过 fgets(name, 100, stdin)
现在我遇到了分段错误,通过阅读我在 google 的前两页找到的每个结果,我的语法看起来是正确的,但我在 cboard 或这里没有找到任何可以修复的东西我的问题。
有什么想法吗?
char *fgets(char *restrict s, int n, FILE *restrict stream);
The fgets() function shall read bytes from stream into the array pointed to by s, until n-1 bytes are read, or a is read and transferred to s, or an end-of-file condition is encountered. The string is then terminated with a null byte. [0]
您需要将其分配到特定大小并使用该大小调用 fgets。此代码可以帮助您完成同样的事情,但它有一个固定大小的缓冲区。
#include <stdlib.h>
#include <stdio.h>
char name;
char* buffer;
int buffer_size = 16;
int i = 0;
void HUD()
{
printf("%s ", buffer);
}
int main(void)
{
buffer = malloc(buffer_size);
if(!buffer) return;
for(;;) {
name = getchar();
if(name < 0) {
buffer[i] = '[=10=]';
goto finish;
} else if(i < (buffer_size -1)) {
buffer[i++] = name;
} else if(name == '\n') {
break;
}
}
buffer[i] = '[=10=]';
finish:
HUD();
free(buffer);
return 0;
}
[0] http://pubs.opengroup.org/onlinepubs/009695399/functions/fgets.html
sizeof(name)
将是您系统上指针的大小,在我的系统上是 8 个字节。不是缓冲区的大小,正如您所期望的那样
另外 char* name
未初始化。您将尝试写入未初始化的缓冲区,它将以未定义的行为结束。
要解决,要么将其设为固定大小的缓冲区,要么在堆上分配一些 space。
分配
#include <stdio.h>
#include <stdlib.h>
#define NAME_SIZE 100
char *name;
void HUD()
{
printf("%s ", name);
}
int main(void)
{
name=calloc(NAME_SIZE, sizeof(char));
fgets(name, NAME_SIZE, stdin);
HUD();
free(name);
}
静态数组
#include <stdio.h>
#include <stdlib.h>
#define NAME_SIZE 100
char name[NAME_SIZE];
void HUD()
{
printf("%s ", name);
}
int main(void)
{
fgets(name, NAME_SIZE, stdin);
HUD();
}
您必须将缓冲区的大小传递给 fgets
,以便它知道必须写入多少 space。