如何使用类型在 Pascal 中初始化数组?
为了好玩,我想刷新我的 Pascal 知识,所以必须再次复习一下基础知识。
让我们看看这段 Java 代码:
class ArrayTest {
public static void main(String args[]){
int[] numArray = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
}
}
将其重写为 Pascal:
type
arrNum = array[1..10] of integer;
var
BBB: arrNum;
begin
BBB := [1,2,3,4,5,6,7,8,8,10];
end.
在 MacOS 上使用 FPC 3.2.2 编译代码,结果是:
arrnum.pas(7,12) 错误:不兼容类型:得到“{Array Of Const/Constant Open} Array > of ShortInt”,预期为“arrNum”
arrnum.pas(10) 致命:编译模块时出现 1 个错误,正在停止
致命:编译中止错误:/usr/local/bin/ppcx64 返回一个 错误退出代码
这里出了什么问题?
For the sake of fun, I'd like to refresh my Pascal knowledge, so gotta review the basics again.
Let's see this Java code:
class ArrayTest {
public static void main(String args[]){
int[] numArray = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
}
}
Rewrite that into Pascal:
type
arrNum = array[1..10] of integer;
var
BBB: arrNum;
begin
BBB := [1,2,3,4,5,6,7,8,8,10];
end.
Compiling the code with FPC 3.2.2 on MacOS, the result is:
arrnum.pas(7,12) Error: Incompatible types: got "{Array Of Const/Constant Open} Array > of ShortInt" expected "arrNum"
arrnum.pas(10) Fatal: There were 1 errors compiling module, stopping
Fatal: Compilation aborted Error: /usr/local/bin/ppcx64 returned an
error exitcode
What's wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用构造函数的类似语法可用于动态阵列(BBB),但不能用于静态阵列。可以通过将类型常数分配给变量(BB10:= CCC10)或定义初始化常数(DDD10)来完成静态初始化。
以下代码证明了某些情况:
A similar syntax using a constructor is available for dynamic arrays (BBB), but not for static ones. Static initializations can be done by assigning a type constant to a variable (BB10:=CCC10) or by defining an initialized constant(DDD10).
The below code demonstrates some cases:
您可以在声明中初始化它:
You can initialize it in the declaration:
在pascal
[1,  2,  3,4,  5,6,6,  7,8,8,  8,8,  10]
set
字面包含9个不同的整数
值。您无法将设置分配给
array
数据类型变量。取而代之的是,您需要指定
array
字面的且正确表示它,但是,如下所示,FPC(尚未)不符合扩展的Pascal规范(ISO Standard 10206)。 。
他们酿造了自己的pascal,因此编译器(“ mis-”)将您的
set
字面标识为“ const/const/constant open open} arnay and and and and and and and and and and and and and and and and and shortint”。参见 marco的答案如何初始化“ ”作为替代方案。
In Pascal
[1, 2, 3, 4, 5, 6, 7, 8, 8, 10]
is aset
literal containing 9 distinctinteger
values.You cannot assign sets to an
array
data type variable.Instead you will need to specify an
array
literal and properly denote it, like so:However, as of version 3.2.0 the FPC does not (yet) comply with the Extended Pascal specification (ISO standard 10206).
They brewed up their own Pascal, so the compiler (“mis‑”)identifies your
set
literal as an “{Array Of Const/Constant Open} Array of ShortInt”.See Marco’s answer how to initialize “dynamic arrays” as an alternative.
如果声明为数组,则应该使用 for 循环来初始化
If you declare as an array you should use a for loop to init
您可以使用 for 循环初始化它
或者
You can initialize it using for loop
or
使用动态数组和Delphi兼容模式,可以使用此语法分配数组:
如果您需要一个静态常数(动态)数组,则分配相似:
Using a dynamic array and delphi compatibility mode, it is possible to assign an array using this syntax:
If you want a static constant (dynamic) array, the assignment is similar: