如何从任意内存块分配记录?

时间:2017-03-03 14:22:55

标签: delphi

我有以下C ++代码,但我仍然坚持如何在Delphi中执行等效的t = *(test *)&memory;

#include <iostream>
using namespace std;

struct test
{
    char a, b, c, d;
};

int main() 
{
    char memory[] = {'a', 'b', 'c', 'd'};
    test t{};
    cout << "Before: " << t.a << t.b << t.c << t.d << endl;
    t = *(test *)&memory;
    cout << "After: " << t.a << t.b << t.c << t.d << endl;
    return 0;
}

输出:

Before:     
After: abcd

http://ideone.com/5y0jzs

1 个答案:

答案 0 :(得分:3)

看看这个:

type
  PTest = ^Test;
  Test = record
    a, b, c, d: AnsiChar;
  end;
const
  Memory: array[0..3] of AnsiChar = ('a', 'b', 'c', 'd');
...
  T := PTest(@Memory)^;

就是这样。     ...