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:
This also works but it's just too stupid:
[/code]
I've tried a lot of { } and , combinations and I couldn't get there.
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.
You have two solutions, so pick one.
Both are ugly :(
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.
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.
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.
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"
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
};
Bwah, ugly! :) but it works..
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:
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:
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:
Your second approach gets closest to what you want, so I'd go with it. Otherwise, change your language.
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.
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]);
}
fr33ke won.
fr33ke: Ah of course, totally forgot about the array types. Thanks. :)
xernobly: Apparently you can write the whole thing in a single go.
xernobly: Apparently you can write the whole thing in a single go.