// 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上适用于我。 – BjoernD
您必须增加可用于应用程序的堆栈大小。但是1111和6829以及5736应该都可以工作。 – RedX