Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to assign values to the whole row of a dynamic two dimensional array?
    text
    copied!<p>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.</p> <p>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.</p> <p>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.</p> <p>What I have tried is:</p> <pre><code>searches-&gt;library[0][0] = {2, 3, 4, -1}; searches-&gt;library[1][0] = {4, 5, 6, -1}; searches-&gt;library[2][0] = {2, 3, 4, 5, 6, -1}; searches-&gt;library[3][0] = {0, 1, 2, 3, 4, 5, 6, 7, -1}; searches-&gt;library[4][0] = {0, 1, 2, -1}; searches-&gt;library[5][0] = {0, 6, 7, -1}; searches-&gt;library[6][0] = {0, 1, 2, 6, 7, -1}; searches-&gt;library[7][0] = {0, 1, 2, 3, 4, -1}; searches-&gt;library[8][0] = {0, 4, 5, 6, 7, -1}; </code></pre> <p>But this gives me <code>p2AdjacencyMatrix.c:179: error: parse error before '{' token</code> for each line.</p> <p>I have also tried:</p> <pre><code>searches-&gt;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}}; </code></pre> <p>And the result it <code>p2AdjacencyMatrix.c:189: error: parse error before ']' token</code></p> <p>Here is the structure definition:</p> <pre><code>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; </code></pre> <p>And the memory allocation for the dynamic array:</p> <pre><code>SearchLibrary* searches; searches = (SearchLibrary *) malloc(sizeof(SearchLibrary*)); int search_cases = 9, search_directions = 9; searches-&gt;library = (int **) malloc(search_cases * sizeof(int *)); searches-&gt;active = (int *) malloc(search_directions * sizeof(int)); int i; for(i = 0; i &lt; search_cases; i++){ searches-&gt;library[i] = (int *) malloc(search_directions * sizeof(int)); } </code></pre> <p><strong>How can I add these values to each row of the array?</strong> 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?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload