Syntax for constant array of array pointers

This is a discussion on Syntax for constant array of array pointers within the C Programming forums, part of the General Programming Boards category; Is it possible to do something like this in C? Code: /* This is a NON-WORKING example. It gives a ...

  1. #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("&#37;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*.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  3. #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.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  5. #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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  2. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  3. using c++ in c code
    By hannibar in forum C Programming
    Replies: 17
    Last Post: 10-28-2005, 10:09 PM
  4. linked list inside array of structs- Syntax question
    By rasmith1955 in forum C Programming
    Replies: 14
    Last Post: 02-28-2005, 05:16 PM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM

Tags for this Thread


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