2

I am reading the RFC 4506 to understand the XDR data definition language.

It mentions that variable-length arrays are declared as follows.

 type-name identifier<m>;

It also mentions that variable-length strings are declared as follows.

 string object<m>;

Unfortunately, the only way it shows to have a variable length array of strings is a linked list, which seems very manual.

struct *stringlist {
    string item<>;
    stringlist next;
};

Is there a more simple or more correct way to declare a variable-length array of strings?

Community
  • 1
  • 1
merlin2011
  • 71,677
  • 44
  • 195
  • 329

1 Answers1

2

You can use the typedef keyword.

typedef does not declare any data either, but serves to define new identifiers for declaring data. The syntax is:

    typedef declaration;

The new type name is actually the variable name in the declaration part of the typedef. For example, the following defines a new type called "eggbox" using an existing type called "egg":

    typedef egg eggbox[DOZEN];    

We can define a variableLengthString type with

typedef string variableLengthString<>;

and then declare a variableLengthString array with

variableLengthString object<>;
Ortomala Lokni
  • 56,620
  • 24
  • 188
  • 240
  • In practice the second typedef was not necessary all the time, (you can just declare an variable-length array of `variableLengthString` objects directly), but this answer is correct regardless. – merlin2011 Mar 23 '15 at 17:04
  • @merlin2011 I've modified my answer according to your remark. – Ortomala Lokni Mar 23 '15 at 19:05