-2

I tried in this way, but I don't know if it's the best way to do this:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define ROW 5
#define COL 5

int *matrix;

int main(int argc, const char * argv[])
{
    matrix = malloc(ROW * COL * sizeof(int));
    memset(matrix, 0, ROW * COL * sizeof(int));

    return 0;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Be more specific about what you're worried your solution might be "not best" in. – einpoklum Dec 25 '20 at 21:29
  • I got the doubt because I have seen some solutions in which each column is allocated with malloc and initialized with memset (in a for loop). –  Dec 25 '20 at 21:33
  • @link23 If the array has equal size in all dimensions, you might as well allocate a one-dimensional array and do some simple math getting the right index. – Cheatah Dec 25 '20 at 21:36
  • @Cheatah: 1. OP is already doing this. 2. This holds even if the sizes in different dimensions are not the same. – einpoklum Dec 25 '20 at 21:39
  • 1
    Does this answer your question? [How do I correctly set up, access, and free a multidimensional array in C?](https://stackoverflow.com/questions/12462615/how-do-i-correctly-set-up-access-and-free-a-multidimensional-array-in-c) – einpoklum Dec 25 '20 at 21:40
  • @einpoklum they are, but they seem to be in doubt. And it does not hold if the sizes in the different dimensions differ, because you would need to know the largest edge size. I think the code is fine, but ideally it should read `matrix = calloc(ROW * COL, *matrix);` – Cheatah Dec 25 '20 at 21:46

2 Answers2

0

(I'm ignoring the word "best" in your question title)

  1. Try to avoid global variables. If you need matrix in main(), then declare it in main() and pass it to other functions as necessary.

  2. As @Vlad's answer mentions - if you specifically want to set your array to zero, you can combine your malloc() and memset() calls into a single calloc() ("clear-allocate" if you will) call.

  3. If you know the matrix dimensions in advance, you might just allocate the matrix on the stack, e.g.:

    int main(int argc, const char * argv[])
    {
        int matrix[ROW * COL] = { 0 };
        // etc. etc.
    }
    

    (which should set the whole array to zero as well.)

    if, on the other hand, the matrix is quite large, then even for dimensions known apriori it makes more sense to allocate dynamically, because stack space is limited.

  4. It is not a good idea to allocate every column or every row separately with malloc(). Even if you wanted your matrix to be used in the form matrix[i][j], you would still use a single allocation - but allocate space for the actual data as well as a sequence of pointers to column beginnings.

Further reading:

einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • Does the expression matrix[ROW * COL] is equal to matrix[ROW][COL]? Furthermore, in this case can't I just write matrix[ROW * COL] = {0};? –  Dec 25 '20 at 21:46
  • @link23: That's a good question. It is _mostly_ equivalent, in the sense that you would get the same memory layout. I didn't recommend it because when you pass such a "pseudo-2D-C-matrix" around, you either pass it as a plain `int*` or you have to include the `COL` parameter in every function's signature. Also, for listing code, use backticks (`). – einpoklum Dec 25 '20 at 21:49
  • 1
    @link23 in this case `matrix2d[x][y]` would be equivalent to either `matrix1d[x * ROW + y]` or `matrix1d[x + y * COL];` as long as you are consistent and always use either the one or the other. – Cheatah Dec 25 '20 at 21:51
0

It is better to deal with a matrix when it is represented as a two-dimensional array.

Instead of calling malloc and then memset manually you can substitute them for a call of calloc.

So you can write in main (there is no need to declare the pointer in the file scope)

int ( *matrix )[COL] = calloc( 1, sizeof( int[ROW][COL] ) );

Using such an array makes code more clear. For example to output the matrix you can write

for ( size_t i = 0; i < ROW; i++ )
{
    for ( size_t j = 0; j < COL; j++ )
    {
        printf( "%d ", matrix[i][j] );
    }
    putchar( '\n' );
}

To conclude about difficulties of using a one-dimensional array imagine how will look a code of multiplication of two matrices.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335