【发布时间】:2013-10-09 10:51:02
【问题描述】:
// C Program to find average of numbers given by user
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main()
{
double sum = 0;
int ii = 0,c;
char buf[256], *token;
printf("Enter the numbers to average on a single line, separated by space and press enter when done\n");
fgets(buf, 255, stdin);
token = strtok(buf, " ");
while (token != NULL)
{
sum += atof(token);
ii++;
token = strtok(NULL, " "); //Get next number
}
printf("Average is %lf", sum / (double)ii);
}
第 8 行:
char buf[256], *token;
当我将数组限制更改为任何 8 位或更多位数时,例如 11111111、68297907(等等...),程序就会被编译,但在输出时会显示 Segmention Error。
如何增加数组限制?我正在使用基于 UNIX 的系统。
【问题讨论】:
-
你可以试试 malloc,但我不明白为什么 1111 不起作用。
-
char buf[4000];在 Linux 上为我工作。
-
您必须增加应用程序可用的堆栈大小。但是 1111 和 6829 和 5736 应该都可以工作。
-
在我的电脑上,char buf[100000] 工作正常。
-
欢迎来到stack overflow。从字面上看。
标签: c arrays segmentation-fault