我需要对一个坐标执行 9 种不同的操作,具体取决于坐标的位置。我有一个函数,它返回给定坐标(下、上、左、右或对角线)周围的位置坐标。9 种不同的操作是不同可能的坐标“类型”;如果我正在处理坐标 (0, 0),则唯一有效的操作是右、右下和下。
我有一个结构,用于存储对每种坐标类型有效的方向。角坐标为 4,所有内部坐标为 1,边缘行的非角列为 4。
我存储所有方向的结构中的字段是一个称为“库”的动态二维数组。库的每一行都对应一种坐标类型,包含该类型坐标的所有有效方向。不过,我还没有找到一种方法来一次分配一行的值,而且我不能用循环单独分配它们。
我尝试过的是:
searches->library[0][0] = {2, 3, 4, -1};
searches->library[1][0] = {4, 5, 6, -1};
searches->library[2][0] = {2, 3, 4, 5, 6, -1};
searches->library[3][0] = {0, 1, 2, 3, 4, 5, 6, 7, -1};
searches->library[4][0] = {0, 1, 2, -1};
searches->library[5][0] = {0, 6, 7, -1};
searches->library[6][0] = {0, 1, 2, 6, 7, -1};
searches->library[7][0] = {0, 1, 2, 3, 4, -1};
searches->library[8][0] = {0, 4, 5, 6, 7, -1};
但这给了我p2AdjacencyMatrix.c:179: error: parse error before '{' token
每一行。
我也试过:
searches->library[][9] = {{2, 3, 4, -1},
{4, 5, 6, -1},
{2, 3, 4, 5, 6, -1},
{0, 1, 2, 3, 4, 5, 6, 7, -1},
{0, 1, 2, -1},
{0, 6, 7, -1},
{0, 1, 2, 6, 7, -1},
{0, 1, 2, 3, 4, -1},
{0, 4, 5, 6, 7, -1}};
结果呢p2AdjacencyMatrix.c:189: error: parse error before ']' token
这是结构定义:
typedef struct{
int active_length; // Size of active array of searches
int* active; // Active array of searches
int** library; // Library of array of searches
} SearchLibrary;
以及动态数组的内存分配:
SearchLibrary* searches;
searches = (SearchLibrary *) malloc(sizeof(SearchLibrary*));
int search_cases = 9, search_directions = 9;
searches->library = (int **) malloc(search_cases * sizeof(int *));
searches->active = (int *) malloc(search_directions * sizeof(int));
int i;
for(i = 0; i < search_cases; i++){
searches->library[i] = (int *) malloc(search_directions * sizeof(int));
}
如何将这些值添加到数组的每一行?我尝试将我的结构定义更改为静态数组,但这也不起作用。这是因为我使用了指向结构的指针吗?