简体   繁体   English

不知道如何处理错误“数组初始值设定项中的元素过多”

[英]Not sure how to deal with the error "excess elements in array initializer"

In line 10 I cannot find out where my problem is at first.在第 10 行中,我一开始无法找出我的问题出在哪里。 I place int a[100][100]={0} but the cpu speed is stuck.我放置了int a[100][100]={0}但 cpu 速度卡住了。
Then, I try to change it into a[n][n] but no output is shown.然后,我尝试将其更改为a[n][n]但未显示任何输出。 Last, I try to change it again as if it resembles the original ones.最后,我尝试再次更改它,就好像它与原始版本相似。 However, nothing works instead of a new question.但是,没有什么可以代替一个新问题。

#include<stdio.h>

int main() {
  int n;

  while (scanf("%d", &n)) {
    n *= 2;
    int x = 0, y = 0, num = 1;
    int a[n][n] = {0};
    a[x][y] = num++;
    while (n * n >= num) //定義陣列
    {
      while (y + 1 < n && !a[x][y + 1]) //向右
        a[x][++y] = num++;

      while (x + 1 < n && !a[x + 1][y]) //向下
        a[++x][y] = num++;

      while (y - 1 >= 0 && !a[x][y - 1]) //向左
        a[x][--y] = num++;

      while (x - 1 >= 0 && !a[x - 1][y]) //向上
        a[--x][y] = num++;
    }
    for (x = 0; x < n; x++) //print 陣列
        {
      for (y = 0; y < n; y++) {
        if (y != n - 1) {
          printf("%d ", a[x][y]);
        } else {
          printf("%d", a[x][y]);
        }
      }
      printf("\n");
    }

    break;
  }

  return 0;
}

At least this problem:至少这个问题:

Variable Length Arrays ( VLA ) cannot be initialized via the C standard.可变长度数组 ( VLA ) 不能通过 C 标准进行初始化。

Alternate, assign via memset() after defining a .互生,经由分配memset()限定后a

// int a[n][n]={0};
int a[n][n];
memset(a, 0, sizeof a);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM