-3

I Have this C code:

typedef enum { STB_Expression_Max} STB_OKAO_EXPRESSION;

typedef struct {STB_INT32 anScore[STB_Expression_Max];} STB_FRAME_RESULT_EXPRESSION;

How can I convert the above code to array in Delphi?

  • 1
    The structure (with the code as shown) will have a zero-sized array, is that by design? Is it supposed to be a [flexible array member](https://en.wikipedia.org/wiki/Flexible_array_member)? – Some programmer dude Jun 05 '17 at 11:33
  • 1
    As for your question, what have you tried? How did (or didn't) your attempt work? What problems do you have with the code you tried? – Some programmer dude Jun 05 '17 at 11:34
  • This doesn't seem unclear. What difficulty do you have. Do you know either of the languages. – David Heffernan Jun 05 '17 at 11:40
  • If you are having problems, and must convert more, please read this: [Pitfalls of converting](http://www.rvelthuis.de/articles/articles-convert.html) – Rudy Velthuis Jun 05 '17 at 13:40
  • @RudyVelthuis In case you are interested, I edited into your deleted answer an explanation as to why an enum was used rather than a C constant. – David Heffernan Jun 06 '17 at 07:31
  • I saw it. I don't think that #defines of simple numerical values are evil (they are more or less like true constants in Pascal), although macros can be, but it is still not what enums are meant for, IMO, even if it is to work around a limitation. Anyway, I'll probably amend the article a bit. FWIW, that is not exactly a flexible array. A flexible array in C99+ is declared as a `[]` array, not as a `[0]` array. The purpose is the same though. – Rudy Velthuis Jun 06 '17 at 10:18
  • I doubt this is really the code you have. I suspect you've omitted other enum values that appeared before that one in the list. That would make the array suitable for holding scores for each of the other kinds of expressions. Since you've removed those declarations, though, the remaining code looks like nonsense. – Rob Kennedy Jun 06 '17 at 12:50
  • @rudy Nothing here suggests C99, and prior to that standard zero length arrays were used for flexible array members. Your opinions on macros aren't relevant to my point. I'm just explaining why you sometimes see enums used that way. You may prefer to use macros. That's fine. But it's a point of fact that enum values are constant expressions. That was the point I was making. It can't be disputed. – David Heffernan Jun 07 '17 at 05:57
  • @David: C99 (IOW, roundabout 18 years ago) was the first standard with the concept of flexible array members, IIRC. And that standard uses `[]` syntax for them. That is why I wrote that. I know why I sometimes see enums that way. I still think it is not what enums are for, even if they are a convenient way to define true constants, and that is why I called it abuse. Single-value enums don't, er, enumerate.IMO, enums with binary values, e.g. 0x0001, 0x0002, 0x0004, 0x0008 etc. are not really proper enums either, especially since they are not used as enums. – Rudy Velthuis Jun 07 '17 at 10:24
  • I'm glad that you understand now – David Heffernan Jun 07 '17 at 11:41
  • @David: I understood from the start. I still think it is abuse of enums, even if I see it being done quite often. – Rudy Velthuis Jun 07 '17 at 17:56
  • You are welcome to your opinion. I just explained the facts. – David Heffernan Jun 07 '17 at 18:00
  • As far as enums with specified ordinality go, bitflags and the like, they aren't enumerations. The feature is badly named. But they get used in that way because they are the best thing for the job. History explains why it is this way. When working with C you have to stop thinking of enum as an enumeration. – David Heffernan Jun 07 '17 at 18:08

1 Answers1

1

It could be:

const
  STB_Expression_Max = 0;

type
  STB_INT32 = ?; { ← define here }
  STB_FRAME_RESULT_EXPRESSION = record
    anScore: array[0..STB_Expression_Max] of STB_INT32;
  end;
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
Victoria
  • 7,822
  • 2
  • 21
  • 44
  • I reverted Rudy's edit, which does not compile. Note that the original answer, whilst compiling, declares an array of length 1, whereas the C code declares a flexible array member. That point needs some explanation. – David Heffernan Jun 05 '17 at 14:32
  • @David: you are right. Don't know what I was thinking. – Rudy Velthuis Jun 06 '17 at 06:59