简体   繁体   中英

I am getting an error in my declaration stated “variably modified 'queue' at file scope”?

Here is a segment of code. What should I do to make queue global and to take its max size from the user?

#include <stdio.h>

int max;
int queue[max];

int main()
{
    scanf("%d",&max);
    return 0;
}

You declared a variable length array in the file scope

int queue[max];

because the number of elements in the declaration is not a constant integer expression.

Objects declared in a file scope have static storage duration. But variable length arrays shall have automatic storage duration. So you may not declare a variable length array in a file scope.

Moreover the variable max used in the array declaration is default initialized by zero. In any case even in a block scope you may not declare a variable length array with the number of elements equal to 0.

From the C Standard (6.7.6.2 Array declarators)

2 If an identifier is declared as having a variably modified type, it shall be an ordinary identifier (as defined in 6.2.3), have no linkage, and have either block scope or function prototype scope. If an identifier is declared to be an object with static or thread storage duration, it shall not have a variable length array type.

and

5 If the size is an expression that is not an integer constant expression: if it occurs in a declaration at function prototype scope, it is treated as if it were replaced by *; otherwise, each time it is evaluated it shall have a value greater than zero. The size of each instance of a variable length array type does not change during its lifetime. Where a size expression is part of the operand of a sizeof operator and changing the value of the size expression would not affect the result of the operator, it is unspecified whether or not the size expression is evaluated.

You should rewrite your program at least like

#include <stdio.h>

int main()
{
    int max;
    scanf("%d",&max);

    int queue[max];

    return 0;
}

Here is a demonstrative program.

#include <stdio.h>

int main(void) 
{
    size_t max;
    
    while ( !( scanf( "%zu", &max ) == 1 && max != 0 ) );
    
    int queue[max];
    
    printf( "The size of the array is %zu\n", sizeof( queue ) );

    return 0;
}

If to enter 10 then the program output might be

The size of the array is 40

that is 40 is the size of the memory extent occupied by the variable length array queue with 10 elements of the type int .

Pay attention to that you may not initialize a variable length array in its declaration.

Instead of the variable length array you could allocate an array dynamically using either standard C function malloc or calloc .

This seems to be a common incorrect assumption among people who just started to learn the language.

int max;
int queue[max];

The size of queue doesn't become bound to the value of max . It's size is fixed as the value of max at the moment when the array is created.

So you need to first read the value to max , and then create queue . This requires you to create queue inside of a function, rather than outside.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM