2

我需要对一个坐标执行 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));
}

如何将这些值添加到数组的每一行?我尝试将我的结构定义更改为静态数组,但这也不起作用。这是因为我使用了指向结构的指针吗?

4

3 回答 3

4

假设 C99,您可以使用复合文字并将memcpy()其覆盖在您的行上。对于k第 - 行,这可能如下所示:

#define SEARCH_DIRECTIONS 9

memcpy(searches->library[k], ((int [SEARCH_DIRECTIONS]){ 1, 2, 3 }),
    sizeof(int) * SEARCH_DIRECTIONS);
于 2009-01-21T15:21:19.727 回答
2

你不能这样做,在 C 中。没有数组文字供你分配,只有数组初始化表达式。

我认为解决方案是简单地从坐标和字段的大小计算所需的值,据我了解这应该很简单。

此外,具有恒定大小的文字初始化值似乎违背了动态分配所有这些内容的观点。

还:

  • 不要在 C 中强制转换 malloc() 的返回值
  • 不必要时不要在类型上使用 sizeof,例如 searchs = malloc(sizeof *searches); 等等
于 2009-01-21T15:10:41.590 回答
2
static const int Library0[] = {2, 3, 4, -1};
static const int Library1[] = {4, 5, 6, -1};
static const int Library2[] = {2, 3, 4, 5, 6, -1};
static const int Library3[] = {0, 1, 2, 3, 4, 5, 6, 7, -1};
static const int Library4[] = {0, 1, 2, -1};
static const int Library5[] = {0, 6, 7, -1};
static const int Library6[] = {0, 1, 2, 6, 7, -1};
static const int Library7[] = {0, 1, 2, 3, 4, -1};
static const int Library8[] = {0, 4, 5, 6, 7, -1};

static const int * Library[] = { 
    Library0, Library1, Library2,
    Library3, Library4, Library5,
    Library6, Library7, Library8,
};

typedef struct{
    int active_length;  // Size of active array of searches
    const int* active;                // Active array of searches
    const int** library;      // Library of array of searches
} SearchLibrary;

searches->library = Library;

编辑:修复语法错误。

于 2009-01-21T15:20:56.717 回答