Thread: Variable length 2d array

  1. #1
    Registered User
    Join Date
    Jul 2015
    Posts
    2

    Variable length 2d array

    Hi guys,

    this is a somewhat philosophical question.

    I was searching for a way to minimize memory usage in an embedded system and come up with this solution:
    Code:
    #include <stdio.h>
    int main()
    {
        char* t[]={"abc","d","ef"}; // Variable length 2d array
        int i;
        for (i=0; i<9; i++)
        {
            printf("%d,",t[0][i]); // Shows bytes in array
        }
        printf("\n");
        printf("%s\n",t[0]); // Shows first string
        printf("%s\n",t[1]); // Shows second string
        printf("%s\n",t[2]); // Shows third string
        printf("\n");
        return 0;
    }
    Here is the output for this code:
    Code:
    sh-4.3$ gcc -o main *.c                                              
    sh-4.3$ main                                               
    97,98,99,0,100,0,101,102,0,
    abc
    d
    ef
    sh-4.3$
    As you can see, the compiler does not created a 3x3 array, but an array with 3 elements, where the first is 4 bytes long, the second is 2 bytes long, and the third is 3 bytes long. Every string has a zero to finalize it.

    Despite this strange format, a reference to a given index works fine. You can get the second element with t[1].

    Here is my question: I always thought C finds an element in a 2d array by multiplying the given references like this:
    Code:
    If I have t[5][5],
    t[3][4] == t[0][3*5+4]
    But how C finds an element in this variable length 2d array that I described here?
    Last edited by Sauloqf; 07-08-2015 at 06:11 AM.

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    You don't have an array with two dimensions. You have an array of pointers.

    Your code may appear to work, but the application doing what you expect is just a coincidence of where the compiler stored the strings.

    The syntax is also not strange.

    Code:
    int x = 0, y = 1, z = 2;
    int s[] = {x, y, z};
    Code:
    const char * x = "abc", * y = "d", * z = "ef";
    const char * s[] = {x, y, z};
    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  3. #3
    Registered User
    Join Date
    Jul 2015
    Posts
    2
    Hi Soma, thank you for your clarification. Now I understand what is going on.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 3dim array, allocation of variable length?
    By tinchi in forum C Programming
    Replies: 5
    Last Post: 06-23-2014, 12:22 PM
  2. variable length array
    By brassbin in forum C Programming
    Replies: 14
    Last Post: 11-14-2013, 12:14 AM
  3. Problem with variable length array
    By skiabox in forum C Programming
    Replies: 11
    Last Post: 11-06-2010, 04:15 PM
  4. variable length array
    By fatsrir in forum C Programming
    Replies: 18
    Last Post: 06-04-2010, 10:52 AM
  5. Variable length array
    By 671 in forum C Programming
    Replies: 2
    Last Post: 09-13-2006, 01:05 PM

Tags for this Thread