neslib/Chet

convert the `struct *` to `TArray<>`

Closed this issue · 1 comments

ccy commented

A c header file:

typedef struct metadata {
   struct metadata_broker *brokers;
} metadata_t;

convert to pascal:

type
  metadata = record
    brokers: pmetadata_broker;
  end;

Is that possible to convert the struct * to TArray<> in pascal?

type
  metadata = record
    brokers: TArray<metadata_broker>;
  end;

That is something you would have to do manually, since this is a potentially dangerous conversion. The TArray<> type is only compatible with a C pointer in very specific situations, and only when the array is used only as input to the C function. C doesn't know anything about the internal structure of a Delphi TArray, so when a C function would write to the "brokers" field, this would result in an access violation on the Delphi side.

Also, pointers in C are not always arrays, but a lot of times just pointers to a single structure. Converting all pointers to arrays would therefore most probably be wrong.

Most of the times, you would have to update the generated Pascal file anyway because conversion is not always possible/accurate. So you can manually convert this field as well if needed.