struct Level_Info
{
char **Map;
} Level[Level_Amount];
for (int Cnt_1 = 0; Cnt_1 < Level_Amount; Cnt_1++)
{
Level[Cnt_1].Map = malloc(Rbn_Col * sizeof(char*));
for (int Cnt_2 = 0; Cnt_2 < Rbn_Col; Cnt_2++)
Level[Cnt_1].*(Map+Cnt_2) = malloc(Rbn_Row * sizeof(char)); /* line 10 */
}
GCC说:expected identifier before 「*」 token
在第10行,那么如何解决呢?
代替
Level[Cnt_1].*(Map+Cnt_2) = malloc(Rbn_Row * sizeof(char));
和
*(Level[Cnt_1].Map+Cnt_2) = malloc(Rbn_Row * sizeof(char));
或简单地与
Level[Cnt_1].Map[Cnt_2] = malloc(Rbn_Row * sizeof(char));
由于sizeof(char)
定义始终为1,因此您也可以这样做
Level[Cnt_1].Map[Cnt_2] = malloc(Rbn_Row);
或在Map
做什么方面保持灵活性
Level[Cnt_1].Map[Cnt_2] = malloc(Rbn_Row * sizeof(Level[Cnt_1].Map[Cnt_2][0]));
另外请注意,索引数组的首选类型size_t
不是int
。
因此,您的代码段应如下所示:
struct Level_Info
{
char ** Map;
} Level[Level_Amount];
for (size_t Cnt_1 = 0; Cnt_1 < Level_Amount; ++Cnt_1)
{
Level[Cnt_1].Map = malloc(Rbn_Col * sizeof(Level[Cnt_1].Map[0]));
for (size_t Cnt_2 = 0; Cnt_2 < Rbn_Col; ++Cnt_2)
{
Level[Cnt_1].Map[Cnt_2] = malloc(Rbn_Row * sizeof(Level[Cnt_1].Map[Cnt_2][0]));
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句