-4

I am trying to write a struct in c that stores data in a linked array like this:

typedef struct
{
    u8 num;
    struct value* next;
}value;

value arr[4] = {
                 {.num = 0, .next = (value[]){
                                              (value){.num = 0, .next = NULL},
                                              (value){.num = 1, .next = NULL},
                                              (value){.num = 31, .next = NULL}
                                             }},
                 {.num = 1, .next = NULL},
                 {.num = 2, .next = NULL},
                 {.num = 31, .next = NULL}
};

Instead of nulls there will be linked array up to 6 hierarchy levels.

How can I do that in C? What am I doing wrong?

Btw, the entire structure should be const so I need to initialize it with values and never touch it again during runtime.

And if you have a method to do that in c++ I can also work with it.

Mike Kinghan
  • 55,740
  • 12
  • 153
  • 182
erano
  • 374
  • 1
  • 6
  • 1
    "How to write a 6 dimentional array of different sizes?" if I had to deal with that I would schedule consultation with a psychotherapist first. 3 dimensions is my maximum. – Slava Feb 02 '18 at 16:08
  • 5
    This is not a multidimensional array, it's a radix tree or trie. Plenty of information is available on radix trees. – Ben Voigt Feb 02 '18 at 16:13
  • 1
    It looks like you are missing a tag `value` on the `struct` type. – Ian Abbott Feb 02 '18 at 16:17
  • 4
    C or C++? It matters here! C++ way would rely on a chain of standard containers: `vector>>>>>`. In C depending of your exact requirements, you could use chained arrays of pointers, chained VLA of pointers, or chained linked lists, or... – Serge Ballesta Feb 02 '18 at 16:25
  • What is the compiler telling you? What is the error message you're getting? What don't you understand about the error message? – Jonathan Leffler Feb 02 '18 at 16:28

2 Answers2

3

I would recommend using a Boost Multidimensional array. In C, memory managements will quickly become unwieldy. See http://www.boost.org/doc/libs/1_63_0/libs/multi_array/doc/user.html

A much messier solution would be to nest std::vectors:

std::vector < std::vector < std::vector< ... > etc.

v2v1
  • 640
  • 8
  • 18
1

Two issues here:

First, your struct doesn't have a tag. So struct value* next; is a pointer to an incomplete type. As a result, you can't create an array of them. You need to add the tag to your struct.

Second, the compound literal syntax on the inner array members isn't needed because you already know you have an array of that type. Also, because this creates separate objects instead of acting as an initializer, this generates errors because you have an object at file scope with initializers that aren't constant. So remove that as well.

After these fixes you should have this:

typedef struct value
{
    u8 num;
    struct value* next;
}value;

value arr[4] = {
                 {.num = 0, .next = (value[]){
                                              {.num = 0, .next = NULL},
                                              {.num = 1, .next = NULL},
                                              {.num = 31, .next = NULL}
                                             }},
                 {.num = 1, .next = NULL},
                 {.num = 2, .next = NULL},
                 {.num = 31, .next = NULL}
};
dbush
  • 205,898
  • 23
  • 218
  • 273
  • That's neat! Also, if OP wants to make everything `const`, then the `next` member would need to be declared as `const struct value *next` (or `struct value const *next`), `value arr[4]` would need changing to `const value arr[4]`, and `(value[]){` would need changing to `(const value[]){`. – Ian Abbott Feb 02 '18 at 16:56