【发布时间】:2020-10-22 19:47:13
【问题描述】:
我在处理指针结构的动态数组时遇到问题。似乎在循环数组的末尾只有 2 个指针。我不知道为什么会这样。我是 C 和低级东西的新手。我在寻求帮助!你能解释一下为什么会这样吗?
#include <stdlib.h>
struct Coordinates {
short x;
short y;
};
const short BOARD_SIZE = 8;
const short MAX_SIZE = 10;
int main() {
struct Coordinates **possible_moves = malloc(MAX_SIZE * sizeof(struct Coordinates));
for (short i = 0; i < BOARD_SIZE; ++i) {
struct Coordinates *current_coordinates = malloc(sizeof(struct Coordinates));
current_coordinates->x = i;
current_coordinates->y = i;
possible_moves[i] = current_coordinates;
}
return 0;
}
【问题讨论】:
-
此声明结构坐标 **possible_moves = malloc(MAX_SIZE * sizeof(struct Coordinates)); 中有错字。你的意思是 sizeof(struct Coordinates *)