pouët.net

Go to bottom

A very simple C related question.

category: code [glöplog]
 
I want to declare a const array of const arrays of different sizes

This works but it's very ugly:
Code:static const char *B[] = { "\x01\x08", "\x02\x04\x09", "\x03\x01\x08\x03", "\x02\x05\x06" }; B[3][1] == 5


This also works but it's just too stupid:
Code:static const unsigned C[] = { 0, 1, 2 }; static const unsigned *A[] = { C };

[/code]

I've tried a lot of { } and , combinations and I couldn't get there.
added on the 2011-03-22 16:25:40 by xernobyl xernobyl
You have two solutions, so pick one.
added on the 2011-03-22 16:36:58 by trc_wm trc_wm
Both are ugly :(
added on the 2011-03-22 16:47:04 by xernobyl xernobyl
Code: static const char *B[] = { "\x01\x08", "\x02\x04\x09", "\x03\x01\x08\x03", "\x02\x05\x06" };

That one will append a \0 to each string.
added on the 2011-03-22 16:51:44 by las las
Going out on a limb here, I may be an idiot.

You want to declare a two-dimensional array. The way C/C++ works when compiling multidimensional arrays is to flatten them, and calculate an index like so:

index = i*width + j

(width signifying the size of the inner array)

Now, this clearly isn't possible if width is unknown or varying. Which it would be in your case.
added on the 2011-03-22 17:06:34 by revival revival
if it is a const array of const arrays of different sizes .... why even bother making that 2 dimensional?

make a 1d const array putting all elements after each other and acces that with array[x] instead of array[y][x]
you can still create some defines for the offset (and size) of each "row" or pass the adress of such an element to a function that needs the value sequence of one "row"

Code: static const unsigned C[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
added on the 2011-03-22 17:10:17 by gopher gopher
Bwah, ugly! :) but it works..
added on the 2011-03-22 18:03:38 by trc_wm trc_wm
xernobyl: Something is wrong. There's no such thing as "const array", you can only have an array of constants (e.g. an array of constant pointers). You can't have the array itself constant because an array is already constant, in that it is not dynamic memory. In C and C++, it's oftentimes cleverer to think this way:

Code: int const *const array[] = { … };


Here we have a variable-length array (whose length is determined at compile time) of constant pointers to constant integers. Hence, "const array" is wrong because:

Code: int const *const array[] const = { … };


This snippet doesn't really make any sense. What does the last const signify? An array is a block of non-dynamic memory whose size is known to the compiler at compile-time. Because of this a "const array" is pointless.

Now, moving on from your example, what you'd like to do is not possible at a single go. las' example is the only exception to this but that's because char (const) * (const) is a special type that can be used to represent a C-string. For any other type, using a list-initialiser for a pointer is not possible; consider this:

Code: int const *const array[] = { reinterpret_cast<int const *const>(0xDEADBEEF), // OK { 0, 1, 2, 3 }, // Ouch… };


Your second approach gets closest to what you want, so I'd go with it. Otherwise, change your language.
added on the 2011-03-22 18:05:29 by decipher decipher
Code:#include <stdio.h> static const char *B[] = { (char[]){1, 8}, (char[]){2, 4, 9}, (char[]){3, 1, 8, 3}, (char[]){2, 5, 6}, }; #define C(...) ((char[]){__VA_ARGS__}) static const char *D[] = { C(1, 8), C(2, 4, 9), C(3, 1, 8, 3), C(2, 5, 6), }; int main(void) { printf("%d\n", B[3][1]); printf("%d\n", D[3][1]); }
added on the 2011-03-22 21:09:42 by fr33ke fr33ke
fr33ke won.
added on the 2011-03-22 21:16:25 by xernobyl xernobyl
fr33ke: Ah of course, totally forgot about the array types. Thanks. :)
xernobly: Apparently you can write the whole thing in a single go.
added on the 2011-03-22 21:19:00 by decipher decipher

login

Go to top