0

I am trying to define a function to return a square matrix (NxN) in C language:

#define true 1
#define false 0
typedef char bool;
typedef bool** Matrix;

Matrix matrix_alloc(int size)
{
  Matrix matrix = (bool **) malloc(sizeof(bool) * size);

  int i;
  for (i = 0; i < size; i++) {
    matrix[i] = (bool *) calloc(size, sizeof(bool));
  }

  return matrix;
}

void matrix_print(Matrix matrix, int size)
{
  int i, j;
  for (i = 0; i < size; i++) {
    for (j = 0; j < size; j++) {
      printf("%i ", matrix[i][j]);
    }
    printf("\n");
  }
}

However, it seems calloc() isn't initializing the "cells" with zero as expected. I was told calloc was safe for initialization, so I believe there is hole in my logics. Here is the output when running the two functions (e.g. to create a 9x9 matrix):

48 77 104 0 72 77 104 0 96
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
J. L. Muller
  • 262
  • 1
  • 3
  • 15
  • 1
    This is not a "bi-dimensional" array. `matrix` is a pointer to an array of pointers to **individual and separate** one-dimensional arrays of pointers to `bool`. See [**Correctly allocating multi-dimensional arrays**](https://stackoverflow.com/questions/42094465/correctly-allocating-multi-dimensional-arrays) – Andrew Henle Aug 30 '20 at 02:12
  • 2
    Use `#include ` to get `bool`, `true` and `false` defined in a standard way. – Jonathan Leffler Aug 30 '20 at 02:59
  • 1
    You might want to read about [hiding pointers behind typedefs](`https://stackoverflow.com/questions/750178/is-it-a-good-idea-to-typedef-pointers`) – Gerhardh Aug 30 '20 at 07:17
  • Pointer to pointer is exactly what I am doing in my code, @AndrewHenle – J. L. Muller Aug 31 '20 at 12:04
  • @J.L.Muller *Pointer to pointer is exactly what I am doing in my code* The title of your question is **"'calloc' on bidimensional array doesn't initialize properly"** – Andrew Henle Aug 31 '20 at 12:39

2 Answers2

2

Wrong size in allocation: matrix is a pointer to bool *, not to bool.

Avoid mistakes, size to the de-referenced pointer, not the type.

The cast is not needed in C, and should be omitted.

//                                  too small
// Matrix matrix = (bool **) malloc(sizeof(bool) * size);
Matrix matrix = malloc(sizeof *matrix * size);
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • It took me a while to understand your point but I got it... Changed the code to `malloc(sizeof(bool *) * size)` (just added the `*` in `sizeof` function and it worked liked a charm! – J. L. Muller Aug 31 '20 at 12:11
  • @J.L.Muller: Coding `sizeof(bool *)` has a more maintenance cost, harder to review and easier to get wrong than `sizeof *matrix`. Your choice. – chux - Reinstate Monica Aug 31 '20 at 14:54
-1
#include <stdlib.h>
#include <stdio.h>

int main(int argc, char **argv, char **envp) {
    int rows=3, cols=2, i, j;
    char *myName = (char *) calloc(rows*cols, sizeof(char));
    char chTemp = 'a';
    //Kh
    //al
    //id
    *(myName)='K'; *(myName+1)='h'; *(myName+2)='a'; *(myName+3)='l'; *(myName+4)='i'; *(myName+5)='d';
    for(i=0; i<rows; i++){
        for(j=0; j<cols; j++){
            chTemp = *(myName+i*cols+j);
            printf("%c", chTemp);
        }
        printf("\n");
    }
    return (EXIT_SUCCESS);
}
LORDTEK
  • 137
  • 11