我有这样的类型:
type
pTRegex_sec=^TRegex_sec;
TRegex_sec = record
secs: Array of pTRegex_sec;
len: byte;
hasSections: boolean;
hasUnits: boolean;
units: Array of TRegex_unit;
end;
type TRegex_assertions = record
len: byte;
secs: Array of TRegex_sec;
end;我想为TRegex_sec类型分配内存:
var Assertions: TRegex_assertions;
begin
setlength(Assertions.secs, 1);
GetMem(Assertions.secs[0], SizeOf(TRegex_sec));
end;我遇到的错误是“不兼容类型”:Assertions.secs[0]<-- here
另一次尝试,同样的错误:
New(Assertions.secs[0]);怎样才能做好呢?
发布于 2019-06-11 17:08:16
TRegex_assertions.secs字段是动态记录数组,而不是指针数组。不需要使用GetMem()来分配数组,SetLength()已经为您处理了。
但是,TRegex_sec.secs字段是一个动态的指针数组。使用SetLength()根据需要分配该数组,然后使用New()分配单个TRegex_sec实例以填充以下内容:
var
Assertions: TRegex_assertions;
begin
SetLength(Assertions.secs, 1);
SetLength(Assertions.secs[0].secs, 1);
New(Assertions.secs[0].secs[0]);
...
end;https://stackoverflow.com/questions/56548289
复制相似问题