In C or C++, you can declare:
const char *foo[] = {
"hello",
NULL,
"cruel",
NULL,
NULL,
"world",
};
Is there any way to make this simple and clean initialization syntax work with arrays of (const) integers instead of arrays of chars, aka string constants, while still allowing for NULL?
So instead of strings like "hello"
and "world"
, could you possibly have some kind of initializer list syntax like &{ 1, 4, 12}
or &({ 0, 4, 9 })
or whatever? Or are strings just a special case in the syntax?
I looked at Initializing “a pointer to an array of integers”, but it didn’t help. Also, it was asked 11 years ago, and maybe a more recent C or C++ standard might provide a way?
In case it’s relevant:
- The integer data is pre-calculated and unchanging.
- For lookup purposes, each pointer to an integer array naturally belongs at a specific index position of the array
foo
of pointers. It is not arbitrary. - For each item in some dataset that the user wants to process, they will use a simple math formula to derive an integer numerical value. So if they derive (for example)
0
from the math, then they need to look infoo[0]
to find a pointer to the corresponding array (for example)1, 4, 12
. - The index positions that contain
NULL
are mathematically impossible and will never be accessed. - Each array of integers will be of the same dimension.
- However nearly 80% of the pointers will be
NULL
, so a 2D array would be wasteful. - The dimensions will be large-ish, but not so large that reading from a file is the only sensible option. Say, an array of 5,000 pointers to arrays of 1,000 integers (with 80% of the pointers being
NULL
instead). The include file that declaresfoo
would be generated automatically.