C 语言中标量初始化器的过量元素警告

Suraj P 2023年10月12日
C 语言中标量初始化器的过量元素警告

在使用C语言时,您可能会遇到一个警告消息,显示excess elements in scalar initializer。这个警告是在我们用太多的元素去初始化数组时出现的。

在本指南中,我们将详细介绍这个警告及其解决方法。

解决C语言中的警告消息 excess elements in scalar initializer

示例代码1:

#include <stdio.h>

int main(void) {
  int array[2][3][4] = {
      {{11, 22, 33}, {44, 55, 66}, {0, 0, 0}},
      {{161, 102, 13}, {104, 15, 16}, {107, 18, 19}},
      {{100, 20, 30, 400}, {500, 60, 70, 80}, {960, 100, 110, 120}}};

  // Your code here

  return 0;
}

输出:

In function 'main':
[Warning] excess elements in array initializer
[Warning] (near initialization for 'array')

上面的错误是因为声明的是int[2][3][4],但我们试图将其初始化为int[3][3][4]

要解决这个错误,我们必须纠正数组的大小。

纠正后的代码(示例1):

#include <stdio.h>

int main(void) {
  int array[3][3][4] = {
      {{11, 22, 33}, {44, 55, 66}, {0, 0, 0, 0}},
      {{161, 102, 13}, {104, 15, 16}, {107, 18, 19, 0}},
      {{100, 20, 30, 400}, {500, 60, 70, 80}, {960, 100, 110, 120}}};

  // Your code here

  return 0;
}

再来看一个例子。

示例代码2:

#include <stdio.h>

int main(void) {
  static char(*check)[13] = {
      {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13},
      {11, 22, 33, 44, 55, 66, 77, 88, 99, 100, 110, 120, 130}};

  // Your code here

  return 0;
}

我们也会得到相同的警告;编译器给出这个警告是因为我们传递了两个指针,但只有一个指向包含13个元素的数组的指针。声明了比必要数量多的元素。

我们可以通过两种方式解决这个问题。

纠正后的代码1(示例2):

#include <stdio.h>

int main(void) {
  // Define two character arrays
  char ar1[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
  char ar2[] = {11, 22, 33, 44, 55, 66, 77, 88, 99, 100, 110, 120, 130};

  // Create an array of character pointers and initialize it with the addresses
  // of ar1 and ar2
  char *check[] = {ar1, ar2};

  // Print the elements in ar1
  printf("ar1: ");
  for (int i = 0; i < sizeof(ar1) / sizeof(ar1[0]); i++) {
    printf("%d ", ar1[i]);
  }
  printf("\n");

  // Print the elements in ar2
  printf("ar2: ");
  for (int i = 0; i < sizeof(ar2) / sizeof(ar2[0]); i++) {
    printf("%d ", ar2[i]);
  }
  printf("\n");

  // Print the elements using the check array of pointers
  printf("check[0]: ");
  for (int i = 0; i < sizeof(ar1) / sizeof(ar1[0]); i++) {
    printf("%d ", check[0][i]);
  }
  printf("\n");

  printf("check[1]: ");
  for (int i = 0; i < sizeof(ar2) / sizeof(ar2[0]); i++) {
    printf("%d ", check[1][i]);
  }
  printf("\n");

  return 0;
}

输出:

ar1: 1 2 3 4 5 6 7 8 9 10 11 12 13 
ar2: 11 22 33 44 55 66 77 88 99 100 110 120 130 
check[0]: 1 2 3 4 5 6 7 8 9 10 11 12 13 
check[1]: 11 22 33 44 55 66 77 88 99 100 110 120 130

纠正后的代码2(示例2):我们只有一个数组指针。该指针指向了一个包含10个元素的数组。我们可以递增数组指针来获取下一个10个元素。

#include <stdio.h>

int main(void) {
  char(*check)[10] = (char[][10]){{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
                                  {0, 31, 29, 31, 30, 31, 30, 31, 31, 30}};

  // Print the elements in the first sub-array
  printf("check[0]: ");
  for (int i = 0; i < 10; i++) {
    printf("%d ", check[0][i]);
  }
  printf("\n");

  // Print the elements in the second sub-array
  printf("check[1]: ");
  for (int i = 0; i < 10; i++) {
    printf("%d ", check[1][i]);
  }
  printf("\n");

  return 0;
}

输出:

check[0]: 1 2 3 4 5 6 7 8 9 10 
check[1]: 0 31 29 31 30 31 30 31 31 30
作者: Suraj P
Suraj P avatar Suraj P avatar

A technophile and a Big Data developer by passion. Loves developing advance C++ and Java applications in free time works as SME at Chegg where I help students with there doubts and assignments in the field of Computer Science.

LinkedIn GitHub