1

Is it possible to define an 2D array in C with variable (but fixed) 2nd dimension? So what I want is more or less something like

    int array[3][] = {{1},{1,2},{1,2,3}}

what does not work. Is there any way to get there where the length of array[0] = 1, array[1] = 2 and array[2]=3?

2
  • Could you clarify what you mean by "variable (but fixed)"? Do you mean you want each row to be a different length, as suggested by your initializer? Commented Jan 28, 2016 at 16:54
  • with variable but fixed i meant, each row may have a different length (variable) but i know in advance the size it should have ("but fixed"). sorry for the confusion Commented Jan 28, 2016 at 17:33

4 Answers 4

3

Is it possible to define an 2D array in C with variable (but fixed) 2nd dimension? So what I want is more or less something like

int array[3][] = {{1},{1,2},{1,2,3}}

[...] Is there any way to get there where the length of array[0] = 1, array[1] = 2 and array[2]=3?

No, there isn't.

C arrays are sequences of elements of the same type. The number and type of the elements are attributes of each array type, not of the array objects having that type. C array indexing depends fundamentally on that.

C 2D arrays are arrays where the element type is a specific array type (with, therefore, a specific number of elements). Thus,

int array[3][2];

declares array to be an array of three elements, each one an array of two ints.

What you can do is have an array of pointers to arrays of various lengths. For example:

int el0[] = { 1 };
int el1[] = { 1, 2 };
int el2[] = { 1, 2, 3 };
int *array[] = { el0, el1, el2 };

In some important respects, you can handle such an array much the same way that you do a bona fide 2D array. Note, however, that that requires either assumptions or separate bookkeeping about the lengths of the pointed-to arrays, and that the three 1D arrays are not necessarily contiguous in memory.

Sign up to request clarification or add additional context in comments.

Comments

3

You can use compound literals. However if the dimensions of the internal arrays are not increased sequentially you have to store the dimensions in some other array.

Here is a demonstrative program

#include <stdio.h>

int main( void )
{
    int *a[] = 
    {
        ( int[] ){ 1 }, ( int [] ){ 1, 2 }, ( int [] ){ 1, 2, 3 }
    };

    for ( size_t i = 0; i < sizeof( a ) / sizeof( *a ); i++ )
    {
        for ( size_t j = 0; j <= i; j++ ) printf( "%d ", a[i][j] );
        printf( "\n" );
    }
}    

Its output is

1 
1 2 
1 2 3 

That is you have an array of pointers each of which points to the first element of unnamed arrays of the appropriate dimensions. In this example the unnamed arrays have the same automatic storage duration as the array of pointers.

1 Comment

thanks vlad, that is actually what john is explaining in the answer i accepted. sry for not choosing yours but johns explanation lights it up more accessible what might help others as well
1

Yo can store your data in a linearized array. The size of the array grows by the row of triangular numbers ( 0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55 ... ). If you have an index [x][y] you can calculae the index in the linearized triangular array by the formula [ y*(y+1)/2 + x ], in which x has to be less or equal y.

int main(void)
{
    int rows = 10;
    int size = rows*(rows+1)/2; // size of linearized array
    int *triangularArray = malloc( sizeof( int ) * size );

    for ( int y = 0; y < rows; y ++ )
    {
        for ( int x = 0; x <= y; x ++ )
        {
            int index = y*(y+1)/2 + x; // linearized index for [x][y] ( x <= y )
            triangularArray[ index ] = x+1;
        }
    }

    for ( int i = 0; i < size; i ++ )
    {
        if ( i > 0 && triangularArray[i] <= triangularArray[i-1] )
            printf( "\n" );
        printf( "%3d", triangularArray[i] );
    }

    free( triangularArray );
    return 0;
}

Comments

0

Will you know ahead of time what the second array's size will be?

You should be able to define a function that takes in the second array's size as input to initialize it with

array buildArr(int size) {

    int[][] newArr = new int [whatever][size]();

    return newArr;

}

3 Comments

That is C++, not C.
no that would leave me an array which has length size in the 2nd dimension, but i want to have the length of the 2nd dimension to be variable (but fixed)
@ryyker, he seems to mean that he wants each (1D array) element of the 2D array to have its own, independent, fixed length.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.