It's been a while since I've had to use C, and I'm getting a warning that I can't figure out regarding a dynamic array of structs. Here's an example of the situation I've got (not real variable names in the first struct, and not as many, but otherwise identical to the set-up I'm using):
Code:
typedef struct {
int a;
int b;
int c;
char *string1;
char *string2;
} *Line;
typedef struct {
int nextAvailIndex;
int maxRows;
Line *tableRows; /* this is a pointer to dynamic array of Lines */
} *Table;
The above info is in a .h file to include, in its corresponding .c I have a new_table function that looks like this:
Code:
Table new_table(int numRows)
{
Table t = (Table) malloc (sizeof (Table));
t -> tableRows = (Line) malloc (sizeof (Line) * numRows);
t -> nextAvailIndex = 0;
t -> maxRows = size;
return t;
}
The problem I'm having is that the green line compiles fine, but for the red line I get:
Code:
warning: assignment from incompatible pointer type
I can fix the warning by changing the red line to:
Code:
*t -> tableRows = (Line) malloc (sizeof (Line) * numRows);
Or I can remove the (Line) and (Table) casts from in front of the mallocs and still compile without problems. The first fix of adding * just doesn't look right to me, and I'm afraid I don't remember enough to understand why I wouldn't have to cast the pointers in the second fix. Any suggestions would be great.