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?