谁能告诉我如何完成这项工作:数组与指针的关系代码

Can anyone tell how can I get this done: code of relation of array with pointers

#include<stdio.h>
    
int main(void){
    const int size=5;
    int grades[size]={34,23,67,89,68};
    double sum=0.0;
    double *ptr_to_sum=&sum;
    int i;
    printf("\n my grades are:\n");
    for(i=0;i<size;i++){
        printf("%d\t",grades[i]);}
        printf("\n\n");
        for(i=0;i<size;i++){
            sum+=grades[i];
        }
        printf("my average grade is %.2f\n\n",sum/size);
        printf("\n\n");
        printf("sum is at %p, or %luandis%lf\n",ptr_to_sum,ptr_to_sum,*ptr_to_sum);
        printf("grades are at %lu to %lu\n",grades,grades+5);
    }

尽管是一个简单的代码,但我无法找出错误,代码是正确的,但我只是不知道为什么会出现此错误。 请问有人可以帮我吗? 经过深思熟虑后,我可以假装它是由于用于求和的数据类型 long 而发生的。

ERROR:pointers.c: In function 'main':
pointers.c:7:5: error: variable-sized object may not be initialized
    7 |     int grades[size]={34,23,67,89,68};
      |     ^~~
pointers.c:7:23: warning: excess elements in array initializer
    7 |     int grades[size]={34,23,67,89,68};
      |                       ^~
pointers.c:7:23: note: (near initialization for 'grades')    
pointers.c:7:26: warning: excess elements in array initializer
    7 |     int grades[size]={34,23,67,89,68};
      |                          ^~
pointers.c:7:26: note: (near initialization for 'grades')    
pointers.c:7:29: warning: excess elements in array initializer
    7 |     int grades[size]={34,23,67,89,68};
      |                             ^~
pointers.c:7:29: note: (near initialization for 'grades')    
pointers.c:7:32: warning: excess elements in array initializer
    7 |     int grades[size]={34,23,67,89,68};
      |                                ^~
pointers.c:7:32: note: (near initialization for 'grades')    
pointers.c:7:35: warning: excess elements in array initializer
    7 |     int grades[size]={34,23,67,89,68};
      |                                   ^~
pointers.c:7:35: note: (near initialization for 'grades') 

在 C 中,如果使用变量作为数组的维度,即使是 const 变量,也会使数组大小可变(在 C++ 中,[=10= 不是这种情况) ]变量)。

因为数组是可变大小的(从编译器的角度来看),它不能初始化数组——因为它假设它不知道有多少元素,所以它不能生成正确的初始化代码。

恐怕您必须在此处求助于宏 - 如果您想为数组指定大小。或者,您可以省略大小,并允许编译器为您推断大小 - 但这需要您初始化数组的 all 元素,如果您给数组大小。

抱怨是你有一个由变量给定的大小(所以它不是常量)和一个初始值设定项列表。这里:

const int size=5;
int grades[size]={34,23,67,89,68};

试试这个:

int grades[] = {34,23,67,89,68};

如果您确实需要 variable/constant 的大小,以下任一方法都可以:

#define NUM_GRADES 5
int grades[] = {34,23,67,89,68};

...

for(i=0;i<NUM_GRADES;i++){

-- 或--

int grades[] = {34,23,67,89,68};
int size = sizeof grades / sizeof grades[0];

来自 C 标准(6.7.6.2 数组声明符):

  1. ... If the size is an integer constant expression and the element type has a known constant size, the array type is not a variable length array type; otherwise, the array type is a variable length array type.

and (6.6 常量表达式)

6 An integer constant expression shall have integer type and shall only have operands that are integer constants, enumeration constants, character constants, sizeof expressions whose results are integer constants, and floating constants that are the immediate operands of casts. Cast operators in an integer constant expression shall only convert arithmetic types to integer types, except as part of an operand to the sizeof operator.

在这些声明中

const int size=5;
int grades[size]={34,23,67,89,68};

变量size不是整数常量表达式。因此数组 grades 是一个变长数组。并根据 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.

因此当数组声明为可变长度数组时,您可能无法初始化数组grades

您可以使用整数常量表达式代替常量变量 size,例如枚举常量:

enum {  size = 5 };
int grades[size]={34,23,67,89,68};

注意这一点,因为您已经有了整数常量 size,而不是此语句中的幻数 5

printf("grades are at %lu to %lu\n",grades,grades+5);

写的会更好

printf( "grades are at %p to %p\n", 
        ( void * )grades, ( void * )( grades + size ) );

要输出指针,您必须使用转换说明符 %p

另一种方法是声明数组grades而不指定元素的数量。例如

int grades[] = { 34, 23, 67, 89, 68 };

然后像

声明一个存储数组元素个数的变量
const size_t size = sizeof( grades ) / sizeof( *grades );