I've found an interesting fact, and I didn't understand how is it works.
The following piece of code just works perfectly.
我发现了一个有趣的事实,我不明白它是如何运作的。以下代码完美无缺。
#include <stdio.h>
int main(){
const int size = 10;
int sampleArray[size];
typedef char String [size];
return 0;
}
Then, I tried to use only and only the constant variable with a global scope, and it's still fine.
然后,我尝试只使用具有全局范围的常量变量,并且它仍然很好。
#include <stdio.h>
const int size = 10;
int main(){
int sampleArray[size];
typedef char String [size];
return 0;
}
But, if I change the arrays's scope to global as well, I got the following:
但是,如果我将数组的范围也改为全局,我得到以下结果:
error: variably modified ‘sampleArray’ at file scope
错误:在文件范围内修改了'sampleArray'
#include <stdio.h>
const int size = 10;
int sampleArray[size];
typedef char String [size];
int main(){
return 0;
}
And I didn't get it! If I'd replace the const variable for ex. to #define
it'd be okay as well.
I know that the #define variable is preprocessed, and as far as I know the const variable is only read-only. But what does make the global scope after all?
我没有得到它!如果我将ex替换为const变量。 #define它也没关系。我知道#define变量是预处理的,据我所知,const变量只是只读的。但究竟什么才能成为全球范围的?
I don't understand what is the problem with the third piece of code, if the second one is just okay.
我不明白第三段代码有什么问题,如果第二段代码还可以。
1 个解决方案
#1
6
Variable Length Arrays may have only automatic storage duration. VLAs were introduced in C99.
可变长度阵列可能只有自动存储持续时间。 VGA在C99中引入。
It is not allowed to declare a VLA with the static storage duration because the size of VLA is determinated at the run time (see below)
不允许声明具有静态存储持续时间的VLA,因为VLA的大小是在运行时确定的(见下文)
Before this Standard you can use either a macro like
在此标准之前,您可以使用宏之类的
#define SIZE 10
//...
int a[SIZE];
or a enumerator of an enumeration like
或枚举的枚举器
enum { SIZE = 10; }
//...
int a[SIZE];
By the way you may remove the const qualifier and just write
顺便说一句,你可以删除const限定符,然后写
int size = 10;
instead of
const int size = 10;
(In C++ you have to use the const qualifier though in C++ there are no VLAs except that some compilers can have their own language extensions)
(在C ++中你必须使用const限定符,尽管在C ++中没有VLA,除了一些编译器可以有自己的语言扩展)
Take into account that the sizeof
operator for VLAs is calculated at the run-time instead of the compile-time.
考虑到VLA的sizeof运算符是在运行时而不是编译时计算的。
#1
6
Variable Length Arrays may have only automatic storage duration. VLAs were introduced in C99.
可变长度阵列可能只有自动存储持续时间。 VGA在C99中引入。
It is not allowed to declare a VLA with the static storage duration because the size of VLA is determinated at the run time (see below)
不允许声明具有静态存储持续时间的VLA,因为VLA的大小是在运行时确定的(见下文)
Before this Standard you can use either a macro like
在此标准之前,您可以使用宏之类的
#define SIZE 10
//...
int a[SIZE];
or a enumerator of an enumeration like
或枚举的枚举器
enum { SIZE = 10; }
//...
int a[SIZE];
By the way you may remove the const qualifier and just write
顺便说一句,你可以删除const限定符,然后写
int size = 10;
instead of
const int size = 10;
(In C++ you have to use the const qualifier though in C++ there are no VLAs except that some compilers can have their own language extensions)
(在C ++中你必须使用const限定符,尽管在C ++中没有VLA,除了一些编译器可以有自己的语言扩展)
Take into account that the sizeof
operator for VLAs is calculated at the run-time instead of the compile-time.
考虑到VLA的sizeof运算符是在运行时而不是编译时计算的。