C 该程序在Linux上生成核心转储,但在Windows上运行良好

C 该程序在Linux上生成核心转储,但在Windows上运行良好,c,C,该程序在Linux上生成核心转储,但在Windows上运行良好。知道为什么吗 #include <stdio.h> int main() { int i, n; int count[n]; int total; int value; int d; printf("Enter the length of array: "); scanf("%d", &n); //printf ("total of array

该程序在Linux上生成核心转储,但在Windows上运行良好。知道为什么吗

#include <stdio.h>

int main() {
    int i, n;
    int count[n];
    int total;
    int value;
    int d;

    printf("Enter the length of array: ");
    scanf("%d", &n);

    //printf ("total of array is %4d \n", n);

    for (i=0; i<=n-1 ; i++ ) {
        printf("Enter the number %d: ", i);
        scanf("%d", &count[i]);
        //  printf ("total of array is %4d \n", n);
    }

    //printf ("total of array is %4d \n", n);

    value = totalcalc( count, n);        
    printf ("total of array is %3d \n", value);

    scanf ("%d", &d);
}

int totalcalc(int count1[], int j)
{
    int  i, total, value;
    //printf (" Entered into function, value of j is %d \n", j);
    value = 0;
    for (i=0; i<=j-1;i++ ) {
        value = value + count1[i];
        //printf ("the value is %d\n", value);
    }
    return value;
}
#包括
int main(){
inti,n;
整数计数[n];
整数合计;
int值;
int d;
printf(“输入数组的长度:”);
scanf(“%d”和“&n”);
//printf(“数组的总数为%4d\n”,n);

对于(i=0;i,因为
int count[n]
是在
n
正确初始化之前声明的。

这一部分非常可疑:

int i, n;
int count[n];
n
显然是单元化的,您正在分配一个大小为
n
的数组

如果需要动态大小的阵列,可以执行以下操作:

int* count;
printf("Enter the length of array: ");
scanf("%d", &n);

count = malloc(n * sizeof(int)); // dynamically allocate n ints on heap

value = totalcalc( count, n);

printf ("total of array is %3d \n", value);

scanf ("%d", &d);

free(count); // free memory

要正确声明数组
count
,请将其声明移动到读取的
n
点之外:

C99唯一解决方案:

int main() 
{
  int i, n;
  int total;
  int value;
  int d;

  printf("Enter the length of array: ");
  scanf("%d", &n);

  int count[n]
#if __STDC_VERSION__ >= 199901L /* going C99 */
# define NEW_ARRAY(t,a,n) t a[n]
# define DELETE_ARRAY(a)
#else
# define NEW_ARRAY(t,a,n) t * a; \ a = malloc((n) * sizeof(t)))
# define DELETE_ARRAY(a) free(a)
#endif

int main() 
{
  int i, n;
  int total;
  int value;
  int d;

  printf("Enter the length of array: ");
  scanf("%d", &n);

  {
    NEW_ARRAY(int, count, n);

    ...

    DELETE_ARRAY(count);
  }

  ...
更灵活的(也包括C99之前的)解决方案:

int main() 
{
  int i, n;
  int total;
  int value;
  int d;

  printf("Enter the length of array: ");
  scanf("%d", &n);

  int count[n]
#if __STDC_VERSION__ >= 199901L /* going C99 */
# define NEW_ARRAY(t,a,n) t a[n]
# define DELETE_ARRAY(a)
#else
# define NEW_ARRAY(t,a,n) t * a; \ a = malloc((n) * sizeof(t)))
# define DELETE_ARRAY(a) free(a)
#endif

int main() 
{
  int i, n;
  int total;
  int value;
  int d;

  printf("Enter the length of array: ");
  scanf("%d", &n);

  {
    NEW_ARRAY(int, count, n);

    ...

    DELETE_ARRAY(count);
  }

  ...

我想保留变量,这样我可以使用n中的任何值,n将决定数组的长度。因此,如果我想使数组的长度为3,那么我将使用n=3,如果我想保持数组的长度为10,那么我将使用n=10。我使用scanf获得n的值。检查输入数是否为负数(或0)也很好,并且分配没有失败。+1 Nevertheless我是C语言的新手,学习C语言非常好,如果你能多解释一下,例如int*count的意思是什么,以及count=malloc(n*sizeof(int))是什么doing?@user1504633:
int*count
声明指向int的指针,该指针用于保存指向足够大的内存块的内存地址,以存储
int
malloc
函数分配指定的字节数,并返回指向新分配的内存块的指针。
n*sizeof(int)
,将
n
乘以
int
的大小(这可能因硬件、系统甚至编译标志的不同而有所不同)。我们也向您推荐。因此,在inet count[n]中使用n之前,我是否应该先阅读n?那么为什么它在窗口上工作?是的。而且您很幸运它运行了。@user1504633如果您声明的是
int count[n];
您依赖于C99功能,如果您的编译器接受C99,则无需打开块。您可以在任何地方声明变量。请看,是的,您是对的。我添加的新块只是为了美观…:-)@PascalCuoqI为了在新块后面添加一些“理性”的含义,我修改了我的答案,如果要构建C99,则使用VLA,否则以经典的方式分配数组。@PascalCuoqI无法更高的投票率,但我肯定会将这些宏放在我的剪辑文件中。这解决了问题:#include int main(){int I,n;printf(输入数组的长度:);scanf(“%d”,&n);printf(“数组的总长度为%4d\n”,n);整数计数[n];