我正在开发一个游戏项目。我的一个结构体包含另一个结构体的矩阵。我无法设法获得malloc作品。这里是我的实际代码:结构矩阵的malloc - C
m->tiles = malloc(sizeof(struct *tile)*width);
for (i=0; i<width ; i++){
m->tiles[i] = malloc(sizeof(struct tile)*height);
}
而且我得到这个错误信息:
map.c:111:37: error: expected ‘{’ before ‘*’ token
m->tiles = malloc(sizeof(struct *tile)*width);
我从来没有过的。已经为int矩阵分配内存,但从未分配结构矩阵。
谢谢。
编辑:谢谢你BLUEPIXY你的答案工作。但我认为我没有很好地定义我的结构:
struct map{
int map_width; // Nombre de tiles en largeur
int map_height; // Nombre de tiles en hauteur
struct tile **tiles; // ensemble des tiles de la map
};
它应该是“struct tile *** tiles”?
'struct * tile' - >'struct tile *'typo – BLUEPIXY 2015-01-26 23:56:41
它不是拼写错误,有两个星号。 – 2015-01-26 23:57:07
'm-> tiles'是什么类型? – 2015-01-27 00:01:05