如何在C中使用已知数字列实现动态2D数组

时间:2022-01-12 02:21:02

I am trying to create a 2d Array at compile time that has an unknown number of rows that i can dynamically allocate throughout the program but a specific number of columns as 8.

我试图在编译时创建一个2d数组,它具有未知的行数,我可以在整个程序中动态分配,但特定数量的列为8。

Something like ---->Elements[?][8];

像----> Elements [?] [8];

5 个解决方案

#1


2  

Knowing the number of columns, and making only the number of rows dynamic you can either use a VLA or dynamic allocation. A VLA is straight forward:

知道列数并仅使行数动态化,您可以使用VLA或动态分配。 VLA很直接:

int rows;

// get rows somehow

int table[rows][8];

Keeping in mind a VLA has automatic storage lifetime and will be removed from addressable memory once the enclosing scope expires. And they cannot be globals.

请记住,VLA具有自动存储生命周期,一旦封闭范围到期,将从可寻址内存中删除。他们不可能是全局的。

If your implementation doesn't support VLA's, automatic storage space is a concern, or you need a global variable for some nefarious purpose, you'll have to manage this dynamically (which it sounds like you want to do anyway). To do that, declare a pointer to an array of 8 elements, as such:

如果你的实现不支持VLA,自动存储空间是一个问题,或者你需要一个全局变量用于某些恶意目的,你必须动态地管理它(听起来你想要做的事情)。为此,声明一个指向8个元素的数组的指针,如下所示:

int rows;

// get rows somehow

int (*table)[8] = malloc(rows * sizeof(*table));

The rest is straight forward. You can reference your elements as table[i][j] for i in 0..rows-1 and j in 0..7. Just remember to free your allocation when finished:

剩下的就是直截了当。您可以在0..rows-1中将您的元素引用为表[i] [j],在0..7中引用j。请记住在完成后释放您的分配:

free(table);

and don't reference it again.

并且不要再次引用它。

#2


3  

If you have to use 2d array instead of list of array you gonna have to make a array

如果你必须使用2d数组而不是数组列表,你将不得不制作一个数组

constant i = 1
foo[i][8]

and every time you want to expand that array

每次你想扩展那个数组

make temp_foo[i][8]
copy foo to temp_foo
delete foo
make foo[i++][8]
copy temp_foo to foo

But that's make confusing. and i think its better if use link list

但那令人困惑。如果使用链接列表,我认为它更好

struct node
{
 foo[8]
 node *next;
}

adding first element

添加第一个元素

node *element_head
element->foo = {add elements}
element->next = null

adding new element

添加新元素

node *temp
temp->foo = {add element}
temp->next = element_head
element_head= temp

#3


1  

As far as I know, you can't have foo[][8] in C. You might be able to hack around it by making a struct and casting a pointer to that struct to an array, as discussed here, but that is a somewhat fragile hack.

据我所知,你不能在C中拥有foo [] [8]。你可以通过制作一个结构并将指向该结构的指针转换为数组来解决它,如此处所讨论的那样,但那是一个有点脆弱的黑客。

What you can do is change the definition of rows and columns in your problem space, so that, in order to access row i, column j, you would do foo[j][i] instead of foo[i][j]. In this case you could declare your array like this: <typename> * foo[8].

您可以做的是更改问题空间中行和列的定义,这样,为了访问第i行,第j列,您将执行foo [j] [i]而不是foo [i] [j]。在这种情况下,您可以像这样声明您的数组: * foo [8]。

#4


1  

I'd go with this approach when the dimensions are unknown.
Assuming data type to be int.

当尺寸未知时,我会采用这种方法。假设数据类型为int。

int* a; //this will point to your 2D array

allocate it when you know the dimensions (ROW, COL):

当你知道尺寸(ROW,COL)时分配它:

a = malloc(sizeof(int)*ROW*COL);

and access it like

并访问它

a[ROW*i + j] = value  // equivalent of a[i][j]

#5


0  

I think it will not be created when you are not passing any value at compile time, my suggestion is to use dynamic memory allocation as you don't know how many rows

我认为当你在编译时没有传递任何值时就不会创建它,我的建议是使用动态内存分配,因为你不知道有多少行

#1


2  

Knowing the number of columns, and making only the number of rows dynamic you can either use a VLA or dynamic allocation. A VLA is straight forward:

知道列数并仅使行数动态化,您可以使用VLA或动态分配。 VLA很直接:

int rows;

// get rows somehow

int table[rows][8];

Keeping in mind a VLA has automatic storage lifetime and will be removed from addressable memory once the enclosing scope expires. And they cannot be globals.

请记住,VLA具有自动存储生命周期,一旦封闭范围到期,将从可寻址内存中删除。他们不可能是全局的。

If your implementation doesn't support VLA's, automatic storage space is a concern, or you need a global variable for some nefarious purpose, you'll have to manage this dynamically (which it sounds like you want to do anyway). To do that, declare a pointer to an array of 8 elements, as such:

如果你的实现不支持VLA,自动存储空间是一个问题,或者你需要一个全局变量用于某些恶意目的,你必须动态地管理它(听起来你想要做的事情)。为此,声明一个指向8个元素的数组的指针,如下所示:

int rows;

// get rows somehow

int (*table)[8] = malloc(rows * sizeof(*table));

The rest is straight forward. You can reference your elements as table[i][j] for i in 0..rows-1 and j in 0..7. Just remember to free your allocation when finished:

剩下的就是直截了当。您可以在0..rows-1中将您的元素引用为表[i] [j],在0..7中引用j。请记住在完成后释放您的分配:

free(table);

and don't reference it again.

并且不要再次引用它。

#2


3  

If you have to use 2d array instead of list of array you gonna have to make a array

如果你必须使用2d数组而不是数组列表,你将不得不制作一个数组

constant i = 1
foo[i][8]

and every time you want to expand that array

每次你想扩展那个数组

make temp_foo[i][8]
copy foo to temp_foo
delete foo
make foo[i++][8]
copy temp_foo to foo

But that's make confusing. and i think its better if use link list

但那令人困惑。如果使用链接列表,我认为它更好

struct node
{
 foo[8]
 node *next;
}

adding first element

添加第一个元素

node *element_head
element->foo = {add elements}
element->next = null

adding new element

添加新元素

node *temp
temp->foo = {add element}
temp->next = element_head
element_head= temp

#3


1  

As far as I know, you can't have foo[][8] in C. You might be able to hack around it by making a struct and casting a pointer to that struct to an array, as discussed here, but that is a somewhat fragile hack.

据我所知,你不能在C中拥有foo [] [8]。你可以通过制作一个结构并将指向该结构的指针转换为数组来解决它,如此处所讨论的那样,但那是一个有点脆弱的黑客。

What you can do is change the definition of rows and columns in your problem space, so that, in order to access row i, column j, you would do foo[j][i] instead of foo[i][j]. In this case you could declare your array like this: <typename> * foo[8].

您可以做的是更改问题空间中行和列的定义,这样,为了访问第i行,第j列,您将执行foo [j] [i]而不是foo [i] [j]。在这种情况下,您可以像这样声明您的数组: * foo [8]。

#4


1  

I'd go with this approach when the dimensions are unknown.
Assuming data type to be int.

当尺寸未知时,我会采用这种方法。假设数据类型为int。

int* a; //this will point to your 2D array

allocate it when you know the dimensions (ROW, COL):

当你知道尺寸(ROW,COL)时分配它:

a = malloc(sizeof(int)*ROW*COL);

and access it like

并访问它

a[ROW*i + j] = value  // equivalent of a[i][j]

#5


0  

I think it will not be created when you are not passing any value at compile time, my suggestion is to use dynamic memory allocation as you don't know how many rows

我认为当你在编译时没有传递任何值时就不会创建它,我的建议是使用动态内存分配,因为你不知道有多少行