5

I need to create array of arrays in static memory, but with different row lengths. I can compute sizes of each row at compile time, but I don't know how to write it down or if it is even possible.

Any ideas please? Thanks in advance ...

kolage
  • 577
  • 9
  • 15
  • 1
    Use an array of pointers. – Fred Foo Nov 21 '13 at 10:01
  • Iarsmans: Thanks, I tried that. But then inside the code, I can't statically allocate memory inside this array. I can't use dynamic allocation, because at this moment, I dont have any and also the array doesn't fit on stack ... – kolage Nov 21 '13 at 10:08

2 Answers2

5

You can't have an array of arrays, because arrays can only have one single type of element, and T[N] and T[M] are different types.

However, you can have an array of pointers:

T a0[5], a1[7], a2[21], a3[2];

T * arr[] = { a0, a1, a2, a3 };

Now you can use arr[0][i] etc.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • OK, thank you, this certainly works, but when I have lot of ak - f.e. k = 20, then I have to write down all a0[...], a1[...], ..., ak[...], right? Is there any way to create them with some macro? – kolage Nov 21 '13 at 10:12
  • We can have array of arrays, but the condition which needs to be met is size and type of each array needs to be same. i.e. say if i do like this ... " typedef int a [5]; a arr[5]" then arr variable is array of 5 arrays, each array is of 5 integer array – Rakesh_Kumar Nov 21 '13 at 10:31
2

You can use an array of pointers that you initialize with compound literals

double* A[] = {
    (double[]){ init00, init012, [45] = init3, },
    (double[]){ init10, init11, init3 },
    (double[34]){ 0.0 },
};

As long as you can guarantee that the initializers and sizes are known at compile time all these allocations will be done statically. The compound literals avoid you to have to declare temporary variables and to polute the namespace of your program.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
  • This doesn't work. clang complains: `pointer is initialized by a temporary array, which will be destroyed at the end of the full-expression`. gcc doesn't seem to give a warning but it should still hold - you're assigning a pointer to point to an array, but the array isn't named so it seems it won't last in memory. – Josh Jul 04 '15 at 04:53
  • @Josh, I don't know what version of clang you have, but this behavior is wrong. Compound literals, just as other objects, live until the end of the scope in which they are declared. So here they have the same lifetime as `A`. The only difficulty with that would be if inside a function you'd declare `A` to be `static`. Then you should have an error "initializer not constant" or so. – Jens Gustedt Jul 04 '15 at 07:11
  • after more research, I think you're right. This seems to agree with you: http://www.drdobbs.com/the-new-c-compound-literals/184401404 . I'm confused because it seems I'm being told something different over here: http://stackoverflow.com/questions/31212114/ – Josh Jul 04 '15 at 07:30
  • @Josh, yes it is different there, because it is talking about a different programming language, namely C++. Don't compile C code with a C++ compiler or the other way around. – Jens Gustedt Jul 04 '15 at 10:41
  • I've never come across this particular difference between C and C++ before. Thanks for enlightening me. It is frustrating that they removed a useful C feature from C++, and did not provide an elegant alternate way of accomplishing the same thing. (I'm currently attempting to accomplish this same thing in C++, hence the frustration.) – Josh Jul 05 '15 at 05:07