簡體   English   中英

不知道如何處理錯誤“數組初始值設定項中的元素過多”

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

在第 10 行中,我一開始無法找出我的問題出在哪里。 我放置了int a[100][100]={0}但 cpu 速度卡住了。
然后,我嘗試將其更改為a[n][n]但未顯示任何輸出。 最后,我嘗試再次更改它,就好像它與原始版本相似。 但是,沒有什么可以代替一個新問題。

#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;
}

至少這個問題:

可變長度數組 ( VLA ) 不能通過 C 標准進行初始化。

互生,經由分配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