![]() |
| | #1 |
| Registered User Join Date: Apr 2008
Posts: 6
| Syntax for constant array of array pointers Code: /* This is a NON-WORKING example. It gives a Segmentation Fault. */
const size_t *foos[4] = {
{3, 1, 2, 3},
{4, 10, 20, 30, 40},
{0},
{1, 100}
};
int main () {
size_t i;
for (i = 0; i < 4; i++) {
size_t *f = foos[i];
size_t j;
for (j = f[0]; j > 0; j--) {
printf("%zu ", f[j]);
}
}
return 0;
}
The intent here is for each array in foos to be allocated by the compiler, and then foos itself would be a 4-element array laid out flatly in memory with each element being the pointer to the first element of its allocated array. In other words, it's a very similar situation to: Code: const char *strs[4] = {
"hello",
"world",
"various",
"strings"
};
Note that I am using the first element of the array to indicate how many elements will be in the array; I'm not asking for any type of bounds checking, only allocation in a manner similar to what is done for arrays of char*. |
| BMintern is offline | |
| | #2 |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Code: const size_t foos[4][5] = {
{3, 1, 2, 3},
{4, 10, 20, 30, 40},
{0},
{1, 100}
};
A list of integers is not an array in and of itself. -- Mats
__________________ Compilers can produce warnings - make the compiler programmers happy: Use them! Please don't PM me for help - and no, I don't do help over instant messengers. |
| matsp is offline | |
| | #3 |
| Registered User Join Date: Apr 2008
Posts: 6
| Thanks for the tip. It's helpful to know that if worst comes to worst, I can do it that way. The only qualms I have are 1. Some of the arrays may be up to 8 or so items long, while the majority will be 3 or less. This means that I'll be using almost twice as much memory as necessary. 2. This involves hand-editing the second value after foos, which could lead to future errors when editing the file. Now in my specific case, I'm actually generating the C code from Python code, so this is less of an issue. All things considered, I'll probably just go this way, but I was curious if there was a more general-purpose way of defining an array of variable-length arrays in the same way that one can define an array of variable-length character buffers. |
| BMintern is offline | |
| | #4 |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Yes, you can do: Code: int foo0[] = { 3, 1, 2, 3 };
int foo1[] = {4, 10, 20, 30, 40};
int foo2[] = {0};
int foo3[] = {1, 100};
int *foos[4] = { foo0, foo1, foo2, foo3 };
Mats
__________________ Compilers can produce warnings - make the compiler programmers happy: Use them! Please don't PM me for help - and no, I don't do help over instant messengers. |
| matsp is offline | |
| | #5 |
| Registered User Join Date: Apr 2008
Posts: 6
| Thanks a lot. I guess that's about as flexible as it gets with C. It sure would be nice to be able to combine the two somehow. |
| BMintern is offline | |
![]() |
| Tags |
| array, const, nested, pointer |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Using VC Toolkit 2003 | Noobwaker | Windows Programming | 8 | 03-13-2006 07:33 AM |
| Connecting to a mysql server and querying problem | Diod | C++ Programming | 8 | 02-13-2006 10:33 AM |
| using c++ in c code | hannibar | C Programming | 17 | 10-28-2005 09:09 PM |
| linked list inside array of structs- Syntax question | rasmith1955 | C Programming | 14 | 02-28-2005 05:16 PM |
| pointers | InvariantLoop | C Programming | 13 | 02-04-2005 09:32 AM |