I need to perform 9 different operations on a coordinate, depending on the position of the coordinate. I have a function that returns the coordinates of a position around the given coordinate (down, up, left, right or diagonals). The 9 different operations are the different possible 'types' of coordinate; if I'm dealing with coordinate (0, 0), the only valid operations are right, down-right and down.
我需要对一个坐标执行9个不同的操作,这取决于坐标的位置。我有一个函数,它返回给定坐标(下,上,左,右或对角线)位置的坐标。这9种不同的操作是不同的可能的坐标类型;如果我处理的是坐标(0,0),唯一有效的操作是正确的,向下和向下。
I have a structure where I store the directions that are valid for each type of coordinate. 4 for the corner coordinates, 1 for all the inner coordinates, and 4 for the non-corner columns of the edge-rows.
我有一个结构,在这个结构中,我存储了对每种坐标都有效的方向。角坐标为4,所有内坐标为1,边行非角列为4。
The field in the structure where I store all the directions is a dynamic two-dimensional array called 'library'. Each row of library would correspond to a type of coordinate, containing all the valid directions for that type of coordinate. I haven't found a way to assign the values one row at a time though, and I can't assign them individually with a loop.
我存储所有方向的结构中的字段是一个名为“library”的动态二维数组。库中的每一行都对应于一种坐标类型,其中包含该坐标类型的所有有效方向。我还没有找到一种方法来一次分配一行的值,而且我不能用循环来单独分配它们。
What I have tried is:
我试过的是:
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};
But this gives me p2AdjacencyMatrix.c:179: error: parse error before '{' token
for each line.
这就得到了p2邻接矩阵。c:179: error:解析每一行的“{”令牌之前的错误。
I have also tried:
我也试过:
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}};
And the result it p2AdjacencyMatrix.c:189: error: parse error before ']' token
结果是它的邻接矩阵。错误:在']'令牌之前解析错误
Here is the structure definition:
结构定义如下:
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;
And the memory allocation for the dynamic array:
以及动态数组的内存分配:
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));
}
How can I add these values to each row of the array? I tried changing my structure definition to a static array, but that didn't work either. Is this happening because I'm using a pointer to a structure?
如何将这些值添加到数组的每一行?我尝试将结构定义更改为静态数组,但这也不起作用。这是因为我使用了一个指向结构的指针吗?
3 个解决方案
#1
1
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;
EDIT: fixed syntax error.
编辑:固定的语法错误。
#2
4
Assuming C99, you can use a compound literal and memcpy()
it over your row. For the k
-th row, this could look like this:
假设C99,您可以使用复合文字和memcpy()在您的行上。对于第k行,它可以是这样的:
#define SEARCH_DIRECTIONS 9
memcpy(searches->library[k], ((int [SEARCH_DIRECTIONS]){ 1, 2, 3 }),
sizeof(int) * SEARCH_DIRECTIONS);
#3
1
You can't do this, in C. There are no array literals for you to assign, there are only array initialization expressions.
你不能这样做,在c中,没有为你分配的数组文字,只有数组初始化表达式。
I think the solution is to simply compute the required value from the coordinates and the size of the field, from my understanding that should be simple.
我认为解决方案是简单地从坐标和场的大小计算所需的值,根据我的理解应该很简单。
Besides, having a constant-sized literal initialization value seems to go against the point of allocating all of this dynamically.
此外,具有常量大小的文字初始化值似乎与动态分配所有这些值的目的背道而驰。
Also:
另外:
- Don't cast the return value of malloc() in C
- 不要在C中强制转换malloc()的返回值
- Don't use sizeof on types when you don't have to, do e.g. searches = malloc(sizeof *searches); and so on
- 不需要时,不要在类型上使用sizeof,例如search = malloc(sizeof *search);等等
#1
1
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;
EDIT: fixed syntax error.
编辑:固定的语法错误。
#2
4
Assuming C99, you can use a compound literal and memcpy()
it over your row. For the k
-th row, this could look like this:
假设C99,您可以使用复合文字和memcpy()在您的行上。对于第k行,它可以是这样的:
#define SEARCH_DIRECTIONS 9
memcpy(searches->library[k], ((int [SEARCH_DIRECTIONS]){ 1, 2, 3 }),
sizeof(int) * SEARCH_DIRECTIONS);
#3
1
You can't do this, in C. There are no array literals for you to assign, there are only array initialization expressions.
你不能这样做,在c中,没有为你分配的数组文字,只有数组初始化表达式。
I think the solution is to simply compute the required value from the coordinates and the size of the field, from my understanding that should be simple.
我认为解决方案是简单地从坐标和场的大小计算所需的值,根据我的理解应该很简单。
Besides, having a constant-sized literal initialization value seems to go against the point of allocating all of this dynamically.
此外,具有常量大小的文字初始化值似乎与动态分配所有这些值的目的背道而驰。
Also:
另外:
- Don't cast the return value of malloc() in C
- 不要在C中强制转换malloc()的返回值
- Don't use sizeof on types when you don't have to, do e.g. searches = malloc(sizeof *searches); and so on
- 不需要时,不要在类型上使用sizeof,例如search = malloc(sizeof *search);等等