# Variable length structures tutorial

Suppose the source text looked like this:

```
struct node_t
{
  long id;
  char *name;
  long nchild;                  // number of children
  long child[];                 // children
};

node_t n0 = { 0, "first",  2, { 1, 2 } };
node_t n1 = { 1, "second", 1, { 3 }    };
node_t n2 = { 2, "third",  1, { 4 }    };
node_t n3 = { 3, "fourth", 0,          };
node_t n4 = { 4, "fifth",  0,          };
      
```

Note that the length of the last field of the structure is not specified. In order to be able to create structures like this in a disassembly we must create a special kind of structure – a variable sized structure. A variable sized structure is created just as a normal structure is. The only difference is that the last member of the structure should be declared as an array with zero elements. (Just a reminder: arrays are declared with an \* hotkey). Here is a sample variable sized structure definition:

![](https://1187734245-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBvvTRYOmg1A3xcvtPL0T%2Fuploads%2Fgit-blob-24f5b257526f6eb93f0776b0c653b6c036aefa91%2FVarstr.gif?alt=media)

Now we may switch to the disassembly window (or just close the enumeration window by pressing Alt-F3). In order to apply the defined structure we use **Edit|Structs|Declare struct var**. But since the structure size cannot be calculated by IDA we need to specify the desired structure size by selecting an area to convert to a structure. Another way to specify the size of a structure would be to use \* hotkey. In all cases you need to tell IDA the exact size of a variable sized structure. The initial disassembly will evolve from this to this:

![](https://1187734245-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBvvTRYOmg1A3xcvtPL0T%2Fuploads%2Fgit-blob-03d89f03edb2dac4f6ff2bf1a7116852e345c7f4%2FBefore-2.gif?alt=media)

![](https://1187734245-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FBvvTRYOmg1A3xcvtPL0T%2Fuploads%2Fgit-blob-a5405a9eaaa4fc1da90c3d3e6b7deb0c6722982f%2FAfter-1.gif?alt=media)

That’s all folks !
