Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

2D array literals in C++

I'm trying to construct a 2D array in the form of pointers-to-pointers. This doesn't work:

bool** data =  {
    new bool[4] {true, true, true, true},
    new bool[4] {true, false, false, true},
    new bool[4] {true, false, false, true},
    new bool[4] {true, true, true, true}
};

Is it possible? How should I be doing it?


EDIT:

Looks like I might be trying to do the wrong thing. I have a function that takes a 2D array of bools of an unknown size, along with integer width and height, as arguments. At present, the signature is:

 foo(bool** data, int width, int height)

I want to be able to construct a literal for data, but I also need this function to work for any size of array.

like image 654
Eric Avatar asked Feb 09 '12 12:02

Eric


People also ask

What is a 2D array in C?

A two-dimensional array in C can be thought of as a matrix with rows and columns. The general syntax used to declare a two-dimensional array is: A two-dimensional array is an array of several one-dimensional arrays. Following is an array with five rows, each row has three columns: int my_array[5][3];

What is 2D array in C example?

Here i and j are the size of the two dimensions, i.e I denote the number of rows while j denotes the number of columns. Example: int A[10][20]; Here we declare a two-dimensional array in C, named A which has 10 rows and 20 columns.

How is a 2 dimensional array initialised?

Like the one-dimensional arrays, two-dimensional arrays may be initialized by following their declaration with a list of initial values enclosed in braces. Ex: int a[2][3]={0,0,0,1,1,1}; initializes the elements of the first row to zero and the second row to one. The initialization is done row by row.

Is array a literal in C?

Compound literals were introduced in C99 standard of C. Compound literals feature allows us to create unnamed objects with given list of initialized values. In the above example, an array is created without any name. Address of first element of array is assigned to pointer p.


2 Answers

You could have an array of arrays (sometimes referred to as a multi-dimensional array):

bool data[][4] =  {
    {true, true, true, true},
    {true, false, false, true},
    {true, false, false, true},
    {true, true, true, true}
};

However, that isn't convertible to bool**, so if you need that conversion then this won't work.

Alternatively, an array of pointers to static arrays (which is convertible to bool**):

bool data0 = {true, true, true, true};
bool data1 = {true, false, false, true};
bool data2 = {true, false, false, true};
bool data3 = {true, true, true, true};
bool * data[] = {data0, data1, data2, data3};

or if you really want dynamic arrays (which is almost certainly a bad idea):

bool * make_array(bool a, bool b, bool c, bool d) {
    bool * array = new bool[4];
    array[0] = a;
    array[1] = b;
    array[2] = c;
    array[3] = d;
    return array;
}

bool * data[] = {
    make_array(true, true, true, true),
    make_array(true, false, false, true),
    make_array(true, false, false, true),
    make_array(true, true, true, true)
};

or, perhaps, you could stick with arrays, and modify your functions to take references to arrays rather than pointers, inferring the dimensions as template parameters if you need to support different dimensions. This is only possible if the dimensions are always known at compile time.

template <size_t N, size_t M>
void do_something(bool (&array)[N][M]);

do_something(data);
like image 114
Mike Seymour Avatar answered Oct 28 '22 15:10

Mike Seymour


bool data_[][4] =  {
     {true, true, true, true},
     {true, false, false, true},
     {true, false, false, true},
     {true, true, true, true}
};
bool *data[4] = { data_[0], data_[1], data_[2], data_[3] };
like image 31
perreal Avatar answered Oct 28 '22 15:10

perreal