1
typedef struct{
       int rows, cols;    // matrix dimensions
       int **element;     // element array
}Matrix;

If i were to create a variable:

Matrix m;

How would i go about creating a 3x3 {{1,2,3},{4,5,6},{7,8,9}} array in the Matrix? Or for that matter, how could i store any sized two-dimensional array into m.element?

I have tried:

for (i=0; i<m.rows; i++)
{
    for (k=0; k<m.cols; k++)
    {
        m.element=q;
        q++;
    }
}
randomusername
  • 7,927
  • 23
  • 50
runnercto
  • 11
  • 2
  • reference this [Dynamic Allocation of 2D Array][1] [1]: http://stackoverflow.com/questions/17583836/dynamic-allocation-of-2d-array/17584120#17584120 – sundq Dec 05 '13 at 02:12
  • http://stackoverflow.com/questions/19572696/read-and-write-matrix/19573110#19573110 – vinay hunachyal Dec 05 '13 at 03:59

3 Answers3

1

You need to allocate space for the matrix. Simply assigning values to m.elements[i][j] will do attempt to access memory at unknown locations, as m.elements will be uninitialized and will have essentially random values. Your program may not be able to access it, or it might not be aligned properly. Build a function to create a rows × cols matrix:

// Initialize and return a passed-in matrix.
// matrix must point to an allocated struct, not NULL.
void build(Matrix * const matrix, const size_t rows, const size_t cols) {
    matrix->rows = rows;
    matrix->cols = cols;
    matrix->elements = malloc(rows * sizeof(int *));
    for (size_t row = 0; row < rows; row++) {
        matrix->elements[row] = malloc(cols * sizeof(int));
    }
}

Note that you can create arrays of any shape. If you need to create symmetric matrices, you need only store items not below the main diagonal.

Since this dynamically allocates the 2-dimensional array, it's your responsibility to free it when you are done:

void destroy(Matrix * const matrix) {
    for (size_t row = 0; row < matrix->rows; row++) {
        free(matrix->elements[row]);
    }
    free(matrix->elements);
}
Eric Jablow
  • 7,874
  • 2
  • 22
  • 29
0

Follow the advice of the comment and look at Dynamic Allocation of 2D Array

Then do

for (i = 0; i < m.rows; i++) {
  for (j = 0; j < m.cols; j++) {
    m.element[i][j] = q++;
  }
}

You need to add in the m.element[i][j] part to make it work.

Community
  • 1
  • 1
randomusername
  • 7,927
  • 23
  • 50
  • That won't work. `m.element` won't be initialized to anything useful. `m.element[i]` may even cause a segmentation fault. `m.element[i][j]` certainly will. – Eric Jablow Dec 05 '13 at 02:13
  • No; the point is that you need to allocate enough space for the array of pointers, and then for each row you need to allocate enough space for its elements. I'll answer below. – Eric Jablow Dec 05 '13 at 02:53
0

Although, you should'nt use pointer to pointer for storing 2-D array, but you can do something like:

m.element = malloc(sizeof(int*)*3);
for (int j=0;j<3;++j)
{
    m.element[j] = malloc(sizeof(int)*3);
}
benipalj
  • 704
  • 5
  • 9
  • A potential problem here is locality of data. Based upon how the OP is iterating over the matrix (I don't know how performance sensitive his code is) he would be better served allocating one large memory chunk and doing the row/column math to look up each element (`matrix[y * cols + x`) – Ed S. Dec 05 '13 at 02:26
  • @EdS. Good point. Didn't think of that. But then probably he would have to change his element from `**` to `*`, right? – benipalj Dec 05 '13 at 02:31
  • the typedef struct cannot be changed, must be ** – runnercto Dec 05 '13 at 02:41
  • This is a student problem, I guess. – Eric Jablow Dec 05 '13 at 03:07
  • @runnercto: then performance doesn't matter and you can just do it this way – Ed S. Dec 05 '13 at 05:55