Why does using const int SIZE = 5 for array size still result in error: variable-sized object may not be initialized?
Why does using const int SIZE = 5 for array size still result in error: variable-sized object may not be initialized?
代码如下:
#include <stdio.h>
int main(void)
{
const int SIZE = 5;
int grades[SIZE]= {72, 65, 54, 39, 79};
double sum = 0.0;
int i;
printf("My grades are: ");
for(i = 0; i < SIZE; i++)
{
printf("%d\t", grades[i]);
}
printf("\n\n");
printf("My grades average is: ");
for(i = 0; i < SIZE; i++)
{
sum = sum + grades[i];
}
printf("%.2f\n\n", sum/SIZE);
return 0;
}
当我尝试编译这段代码时,我得到:
average.c: In function 'main':
average.c:12:5: error: variable-sized object may not be initialized
12 | int grades[SIZE]= {72, 65, 54, 39, 79};
| ^~~
average.c:12:24: warning: excess elements in array initializer
12 | int grades[SIZE]= {72, 65, 54, 39, 79};
| ^~
average.c:12:24: note: (near initialization for 'grades')
average.c:12:28: warning: excess elements in array initializer
12 | int grades[SIZE]= {72, 65, 54, 39, 79};
| ^~
average.c:12:28: note: (near initialization for 'grades')
average.c:12:32: warning: excess elements in array initializer
12 | int grades[SIZE]= {72, 65, 54, 39, 79};
| ^~
average.c:12:32: note: (near initialization for 'grades')
average.c:12:36: warning: excess elements in array initializer
12 | int grades[SIZE]= {72, 65, 54, 39, 79};
| ^~
average.c:12:36: note: (near initialization for 'grades')
average.c:12:40: warning: excess elements in array initializer
12 | int grades[SIZE]= {72, 65, 54, 39, 79};
| ^~
average.c:12:40: note: (near initialization for 'grades')
我的理解是,声明和初始化 const int SIZE = 5
意味着 SIZE
是一个常量,没有可变大小。我已经看到了同一个问题的答案,但我不明白 const int SIZE
为什么不能使 SIZE
成为常量整数表达式。此外,我正在使用 const int SIZE = 5
学习 C 编程的课程,并且程序已正确编译和 运行。
为什么 const int SIZE = 5
不正确,什么是正确的?
为什么不能用SIZE as #define SIZE ((uint8_t)5),这也是常量初始化,它是预处理器,所以它想要在 运行 时间环境中改变,
代码如下:
#include <stdio.h>
int main(void)
{
const int SIZE = 5;
int grades[SIZE]= {72, 65, 54, 39, 79};
double sum = 0.0;
int i;
printf("My grades are: ");
for(i = 0; i < SIZE; i++)
{
printf("%d\t", grades[i]);
}
printf("\n\n");
printf("My grades average is: ");
for(i = 0; i < SIZE; i++)
{
sum = sum + grades[i];
}
printf("%.2f\n\n", sum/SIZE);
return 0;
}
当我尝试编译这段代码时,我得到:
average.c: In function 'main':
average.c:12:5: error: variable-sized object may not be initialized
12 | int grades[SIZE]= {72, 65, 54, 39, 79};
| ^~~
average.c:12:24: warning: excess elements in array initializer
12 | int grades[SIZE]= {72, 65, 54, 39, 79};
| ^~
average.c:12:24: note: (near initialization for 'grades')
average.c:12:28: warning: excess elements in array initializer
12 | int grades[SIZE]= {72, 65, 54, 39, 79};
| ^~
average.c:12:28: note: (near initialization for 'grades')
average.c:12:32: warning: excess elements in array initializer
12 | int grades[SIZE]= {72, 65, 54, 39, 79};
| ^~
average.c:12:32: note: (near initialization for 'grades')
average.c:12:36: warning: excess elements in array initializer
12 | int grades[SIZE]= {72, 65, 54, 39, 79};
| ^~
average.c:12:36: note: (near initialization for 'grades')
average.c:12:40: warning: excess elements in array initializer
12 | int grades[SIZE]= {72, 65, 54, 39, 79};
| ^~
average.c:12:40: note: (near initialization for 'grades')
我的理解是,声明和初始化 const int SIZE = 5
意味着 SIZE
是一个常量,没有可变大小。我已经看到了同一个问题的答案,但我不明白 const int SIZE
为什么不能使 SIZE
成为常量整数表达式。此外,我正在使用 const int SIZE = 5
学习 C 编程的课程,并且程序已正确编译和 运行。
为什么 const int SIZE = 5
不正确,什么是正确的?
为什么不能用SIZE as #define SIZE ((uint8_t)5),这也是常量初始化,它是预处理器,所以它想要在 运行 时间环境中改变,