C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 05-14-2008, 07:34 AM   #1
Registered User
 
Join Date: Apr 2008
Posts: 6
Syntax for constant array of array pointers

Is it possible to do something like this in C?

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 expected output: 3 2 1 40 30 20 10 100

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"
};
In this case, the compiler acts exactly as I described, it allocates various char buffers in memory and then strs is 4 consecutive items in memory with each pointing to the first element of its char buffer. Is there not a more general way to do this for any array similar to the syntax I've used above? It is important that the inner arrays are able to be of variable length, just as with a char* array.

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   Reply With Quote
Old 05-14-2008, 07:39 AM   #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}
};
would work. Obviously 5 should be replaced with the largest number of entries for one row.

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   Reply With Quote
Old 05-14-2008, 07:52 AM   #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   Reply With Quote
Old 05-14-2008, 08:04 AM   #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   Reply With Quote
Old 05-14-2008, 08:21 AM   #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   Reply With Quote
Reply

Tags
array, const, nested, pointer

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 07:17 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22