Home > Software design >  C: Constant length doesn't allow me to declare array
C: Constant length doesn't allow me to declare array

Time:04-04

I'm experiencing this annoying issue when I try to compile a simple code to understand pointers. Basically, the error happens in the declaration of the array:

// Use of pointers

#include <stdio.h>

int main(void) {
    const int SIZE = 5;

    int grades[SIZE]={78, 80, 75, 82, 83};
    //memset( grades, 0, SIZE*sizeof(int) );
    double sum = 0.0;
    double *ptr_sum = &sum;
    int i;

    printf("My grades are: \n");
    for (i=0; i<SIZE; i  ) {
        printf("%d\n",grades[i]);
        sum = sum   grades[i];
    }
    printf("\n\n");
    printf("My average is %.2f\n\n", sum/SIZE);

    printf("sum is at %p, or %lu and is %lf\n",
        ptr_sum, ptr_sum, *ptr_sum);
    printf("Grades are at %lu to %lu\n", grades, grades 5);
    return 0;
}

After trying to compile, the error is the following:

pointers.c:8:5: error: variable-sized object may not be initialized int grades[SIZE]={78, 80, 75, 82, 83};

If I'm using a constant for the length of my array, why is this happening?

If I remove the array content (including the brackets), the program compiles. If I leave the array empty it fails again.

CodePudding user response:

The array is considered a variable length array because its size is not a compile time constant, and a const variable doesn't qualify as such. And because it is a variable length array, it cannot be initialized.

You would need to make SIZE a macro so that a direct token substitution is done.

#define SIZE 5

CodePudding user response:

In C opposite to C using a constant object in a declaration of an array like this

const int SIZE = 5;

int grades[SIZE]={78, 80, 75, 82, 83};

declares a variable length array that may not be initialized in its declaration.

Instead you need to use an integral constant expression.

From the C Standard (6.6 Constant expressions)

6 An integer constant expression117) 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.

In this quote the term "integer constants" means integer literals not objects declared with the qualifier const.

You can declare the array for example the following way

enum { SIZE = 5 };

int grades[SIZE]={78, 80, 75, 82, 83};

//...

Or alternatively you can introduce a macro like

#define SIZE 5

//...

int grades[SIZE]={78, 80, 75, 82, 83};

//...

Also these call of prinf

printf("sum is at %p, or %lu and is %lf\n",
    ptr_sum, ptr_sum, *ptr_sum);

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

are incorrect. You are trying to output a pointer using the conversion specifier lu. You need to use the conversion specifier p. For example

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

Or you could at first declare the array without specifying its size and then introduce the constant that specifiers its size as for example

int grades[]={78, 80, 75, 82, 83};
const size_t SIZE - sizeof( grades ) / sizeof( *grades );
  • Related