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

Even though being a simple code , I am unable to figure out the error ,the code is correct but I just don't know why this error is coming. Please can anyone help me in this? What I can just pretend after much thinking is that it is occurring due to the datatype long that is being used for the sum.

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') 
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Sparkle
  • 9
  • 1
  • 2

3 Answers3

1

In C, if you use a variable as a dimension of an array, even a const variable, it makes the array variable-sized (In C++, that is not the case for const variables).

Because the array is variable-sized (from the compiler's perspective), it can not initialize the array - because it assumes it doesn't know how many elements are there, so it can not generate proper initialization code.

You will have to resort to macros here, I am afraid - if you want to give the array a size. Alternatively, you can omit the size, and allow the compiler to deduce the size for you - but this would require you to initialize all elements of the array, which would not be necessary if you give the array a size.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
SergeyA
  • 61,605
  • 5
  • 78
  • 137
0

The complaint is that you have both a size, given by a variable (so it's not constant), and a list of initializers. Here:

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

Try this instead:

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

If you really need the size as a variable/constant, either of these will work:

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

...

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

-- OR --

int grades[] = {34,23,67,89,68};
int size = sizeof grades / sizeof grades[0];
ash
  • 4,867
  • 1
  • 23
  • 33
0

From the C Standard (6.7.6.2 Array declarators):

  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 Constant expressions)

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.

That is in these declarations

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

the variable size is not an integer constant expression. Thus the array grades is a variable length array. And according to the C Standard (6.7.9 Initialization)

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.

So you may mot initialize the array grades when it is declared as a variable length array.

Instead of the constant variable size you could use an integer constant expression as for example an enumeration constant:

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

Pay attention to that as you already have the integer constant size then instead of the magic number 5 in this statement

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

it will be better to write

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

To output pointers you have to use the conversion specifier %p.

Another approach is to declare the array grades without specifying the number of elements. For example

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

and then to declare a variable that stores the number of elements in the array like

const size_t size = sizeof( grades ) / sizeof( *grades );
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335