Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiler error: incompatible types when assigning to 'struct' from type 'void *' during malloc

EDIT -- can the down voter explain? I have a clear question, with supporting evidence, and proof of prior investigation. I would like to understand why you are down voting me...?


I am getting this error when I compile with gcc:

error: incompatible types when assigning to type ‘struct cell’ from type ‘void *

The problem lines are:

    struct cell* cells = NULL;
    cells = malloc(sizeof(struct cell) * length);
    for (i = 0; i < length; i++) {
            cells[i] = malloc(sizeof(struct cell) * width);

I believe I have followed the proper protocol, as described here and also here. What am I missing?

like image 289
d0rmLife Avatar asked Feb 20 '13 15:02

d0rmLife


1 Answers

For a multidimensional array, you want an array of type struct cell** cells:

struct cell** cells = NULL;
cells = malloc(sizeof(struct cell*) * length);
for(int i = 0; i < length; i++) {
  cells[i] = malloc(sizeof(struct cell)*width);
}

Now cells is a multidimensional array, where the first index range is the length and the second index range is the width.

like image 130
John Colanduoni Avatar answered Sep 30 '22 07:09

John Colanduoni