bob esponja
bob esponja

Reputation: 4139

How to assign values to the whole row of a dynamic two dimensional array?

I need to perform 9 different operations on a coordinate, depending on the position of the coordinate. I have a function that returns the coordinates of a position around the given coordinate (down, up, left, right or diagonals). The 9 different operations are the different possible 'types' of coordinate; if I'm dealing with coordinate (0, 0), the only valid operations are right, down-right and down.

I have a structure where I store the directions that are valid for each type of coordinate. 4 for the corner coordinates, 1 for all the inner coordinates, and 4 for the non-corner columns of the edge-rows.

The field in the structure where I store all the directions is a dynamic two-dimensional array called 'library'. Each row of library would correspond to a type of coordinate, containing all the valid directions for that type of coordinate. I haven't found a way to assign the values one row at a time though, and I can't assign them individually with a loop.

What I have tried is:

searches->library[0][0] = {2, 3, 4, -1};
searches->library[1][0] = {4, 5, 6, -1};
searches->library[2][0] = {2, 3, 4, 5, 6, -1};
searches->library[3][0] = {0, 1, 2, 3, 4, 5, 6, 7, -1};
searches->library[4][0] = {0, 1, 2, -1};
searches->library[5][0] = {0, 6, 7, -1};
searches->library[6][0] = {0, 1, 2, 6, 7, -1};
searches->library[7][0] = {0, 1, 2, 3, 4, -1};
searches->library[8][0] = {0, 4, 5, 6, 7, -1};

But this gives me p2AdjacencyMatrix.c:179: error: parse error before '{' token for each line.

I have also tried:

searches->library[][9] = {{2, 3, 4, -1},
                         {4, 5, 6, -1},
                         {2, 3, 4, 5, 6, -1},
                         {0, 1, 2, 3, 4, 5, 6, 7, -1},
                         {0, 1, 2, -1},
                         {0, 6, 7, -1},
                         {0, 1, 2, 6, 7, -1},
                         {0, 1, 2, 3, 4, -1},
                         {0, 4, 5, 6, 7, -1}};

And the result it p2AdjacencyMatrix.c:189: error: parse error before ']' token

Here is the structure definition:

typedef struct{
    int active_length;  // Size of active array of searches
    int* active;        // Active array of searches
    int** library;  // Library of array of searches
} SearchLibrary;

And the memory allocation for the dynamic array:

SearchLibrary* searches;
searches = (SearchLibrary *) malloc(sizeof(SearchLibrary*));
int search_cases = 9, search_directions = 9;
searches->library = (int **) malloc(search_cases * sizeof(int *));
searches->active = (int *) malloc(search_directions * sizeof(int));

int i;
for(i = 0; i < search_cases; i++){
    searches->library[i] = (int *) malloc(search_directions * sizeof(int));
}

How can I add these values to each row of the array? I tried changing my structure definition to a static array, but that didn't work either. Is this happening because I'm using a pointer to a structure?

Upvotes: 2

Views: 4062

Answers (3)

Die in Sente
Die in Sente

Reputation: 9937

static const int Library0[] = {2, 3, 4, -1};
static const int Library1[] = {4, 5, 6, -1};
static const int Library2[] = {2, 3, 4, 5, 6, -1};
static const int Library3[] = {0, 1, 2, 3, 4, 5, 6, 7, -1};
static const int Library4[] = {0, 1, 2, -1};
static const int Library5[] = {0, 6, 7, -1};
static const int Library6[] = {0, 1, 2, 6, 7, -1};
static const int Library7[] = {0, 1, 2, 3, 4, -1};
static const int Library8[] = {0, 4, 5, 6, 7, -1};

static const int * Library[] = { 
    Library0, Library1, Library2,
    Library3, Library4, Library5,
    Library6, Library7, Library8,
};

typedef struct{
    int active_length;  // Size of active array of searches
    const int* active;                // Active array of searches
    const int** library;      // Library of array of searches
} SearchLibrary;

searches->library = Library;

EDIT: fixed syntax error.

Upvotes: 2

Christoph
Christoph

Reputation: 169603

Assuming C99, you can use a compound literal and memcpy() it over your row. For the k-th row, this could look like this:

#define SEARCH_DIRECTIONS 9

memcpy(searches->library[k], ((int [SEARCH_DIRECTIONS]){ 1, 2, 3 }),
    sizeof(int) * SEARCH_DIRECTIONS);

Upvotes: 4

unwind
unwind

Reputation: 399863

You can't do this, in C. There are no array literals for you to assign, there are only array initialization expressions.

I think the solution is to simply compute the required value from the coordinates and the size of the field, from my understanding that should be simple.

Besides, having a constant-sized literal initialization value seems to go against the point of allocating all of this dynamically.

Also:

  • Don't cast the return value of malloc() in C
  • Don't use sizeof on types when you don't have to, do e.g. searches = malloc(sizeof *searches); and so on

Upvotes: 2

Related Questions