初始化数组时遇到问题

Facing problem while initializing an array

我在用 C 语言初始化数组时遇到问题。我正在尝试输入一个变量 'n' 并声明一个数组 marks[n] 并将其值设置为零。我编写了程序的以下部分来完成它。

int n,k,e,m,x;
scanf("%d %d %d %d", &n,&k,&e,&m);
int marks[n]={0};

但是执行该程序会产生以下警告和错误:

prog.c: In function ‘main’:

prog.c:10:6: error: variable-sized object may not be initialized
      int marks[n]={0};
      ^~~

prog.c:10:20: warning: excess elements in array initializer
      int marks[n]={0};
                    ^

prog.c:10:20: note: (near initialization for ‘marks’)

这是整个程序:

    #include <stdio.h>
int main(int argc, char const *argv[])
{
int t;
scanf("%d",&t);
for (int z = 0; z < t; ++z)
{
    int num_candidate,num_seat,num_exam,max_mark,mark_needed;
    scanf("%d %d %d %d", &num_candidate,&num_seat,&num_exam,&max_mark);
    int marks[num_candidate]={0};
  /*gets the total marks of each students. mark of the last exam of the */
  /*num_candidate-th student is not taken */
    for (int i = 0; i < num_candidate; ++i)
    {
        for(int j=0;j<num_exam;j++)
        {
            if (i==num_candidate-1 && j==num_exam-1)
            {
                break;
            }
            scanf("%d",&mark_needed);
            marks[i]=marks[i]+mark_needed;
        }
    }
             /*sorting*/
    for (int i = 0; i < num_candidat-2; i++)
    {
        for(int j=i; j<num_candidat-2; j++)
        {
            if (marks[j]<marks[j+1])
            {
                int temp = marks[j];
                marks[j]= marks[j+1];
                marks[j+1]=temp;
            }
        }
    }
        /*prints the needed marks*/
    mark_needed=marks[num_seat-1]-marks[num_candidat-1];
    printf("%d\n",mark_needed+1 );
}


return 0;
}

我的目标是参加num_candidate=考生人数,num_seat=学校名额,num_exam=考试人数,max_mark=最大单次考试可达到的分数。 我想知道第 n 个学生在期末考试中需要多少分才能被录取。他上次考试的分数没有作为程序的输入,我想算出他在期末考试中需要的最少分数。

我该如何解决?

来自C标准(6.7.9初始化)

3 The type of the entity to be initialized shall be an array of unknown size or a complete object type that is not a variable length array type.

所以用初始化器代替这个声明

int marks[n]={0};

使用

int marks[n];
memset( marks, 0, n * sizeof( int ) );

注意n可能不等于0