Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: "variably modified 'd' at file scope"

Code 1:

int size;

struct demo
{
    int a;
};

int main()
{
    scanf("%d",&size);
    struct demo d[size];
    return 0;
}

This code works fine.

Code 2:

int size;

struct demo
{
    int a;
};

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

struct demo d[size];

This code shows an error:

error : variably modified 'd' at file scope

Why such error is coming in Code 2 whereas Code 1 runs fine?

like image 992
kevin gomes Avatar asked Dec 08 '25 22:12

kevin gomes


1 Answers

In code 2, your array of structs resides in data segment which is by definition

A data segment is a portion of virtual address space of a program, which contains the global variables and static variables that are initialized by the programmer. The size of this segment is determined by the values placed there by the programmer before the program was compiled or assembled, and does not change at run-time.

like image 61
Dabo Avatar answered Dec 11 '25 12:12

Dabo