Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Statically speaking</strong>, this is easy to understand:</p> <pre><code>int mtx[3][2] = {{1, 2}, {2, 3}, {3, 4}}; </code></pre> <p>Nothing complicated here. 3 rows, 2 columns; data in column one: <code>1, 2, 3</code>; data in column two: <code>2, 3, 4</code>. We can access the elements via the same construct:</p> <pre><code>for(i = 0; i&lt;3; i++){ for(j = 0; j&lt;2; j++) printf("%d ", mtx[i][j]); printf("\n"); } //output //1 2 //2 3 //3 4 </code></pre> <p>Now let’s look at this in terms of <strong>Pointers</strong>: </p> <p>The brackets are a very nice construct to help simplify things, but it doesn’t help when we need to work in a dynamic environment, so we need to think of this in terms of pointers. If we want to store a “row” of integers, we need an array:</p> <pre><code>int row[2] = {1,2}; </code></pre> <p>And you know what? We can access this just like a pointer. </p> <pre><code>printf("%d, %d\n",*row,*(row+1)); //prints 1, 2 printf("%d, %d\n",row[0],row[1]); //prints 1, 2 </code></pre> <p>Now if we don’t know the number of values in a row we can make this array a dynamic length if we have a pointer to int, and we give it some memory:</p> <pre><code>int *row = malloc(X * sizeof(int)); //allow for X number of ints *row = 1; //row[0] = 1 *(row+1) = 2; //row[1] = 2 … *(row+(X-1)) = Y; // row[x-1] = Some value y </code></pre> <p>So now we have a dynamic 1 dimensional array; a single row. But we want lots of rows, not just one, and we don’t know how many. That means we need another dynamic 1 dimensional array, each element of that array will be a pointer which points to a row. </p> <pre><code>//we want enough memory to point to X number of rows //each value stored there is a pointer to an integer int ** matrix = malloc(X * sizeof(int *)); //conceptually: (ptr to ptr to int) (pointer to int) **matrix ------------&gt; *row1 --------&gt; [1][2] *row2 --------&gt; [2][3] *row3 --------&gt; [3][4] </code></pre> <p>Now all that’s left to do is to write the code which will perform these dynamic allocations:</p> <pre><code>int i, j, value = 0; //allocate memory for the pointers to rows int ** matrix = malloc(Rows * sizeof(int*)); //each row needs a dynamic number of elements for(i=0; i&lt;Rows; i++){ // so we need memory for the number of items in each row… // we could call this number of columns as well *(matrix + i) = malloc(X * sizeof(int)); //While we’re in here, if we have the items we can populate the matrix for(j=0; j&lt;X; j++) *(*(matrix+i)+j) = value; // if you deference (matrix + i) you get the row // if you add the column and deference again, you // get the actual item to store (not a pointer!) } </code></pre> <p>One of the most important things to do now is to make sure we free the memory when we’re done. Each level of <code>malloc()</code> should have the same number of <code>free()</code> calls, and the calls should be in a FILO order (reverse of the malloc calls):</p> <pre><code>for(i=0; i&lt;Rows; i++) free(*(matrix + i)); free(matrix); //set to NULL to clean up, matrix points to allocated memory now so let’s not use it! matrix = NULL; </code></pre>
 

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