gpt4 book ai didi

c - 如何在 C 中实现具有已知数字列的动态二维数组

转载 作者:太空宇宙 更新时间:2023-11-04 05:26:49 25 4
gpt4 key购买 nike

我正在尝试在编译时创建一个二维数组,该数组的行数未知,我可以在整个程序中动态分配这些行,但列数为 8。

类似 ---->Elements[?][8];

最佳答案

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

constant i = 1
foo[i][8]

每次你想扩展那个数组时

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

但这让人感到困惑。我认为如果使用链接列表会更好

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

添加第一个元素

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

添加新元素

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

关于c - 如何在 C 中实现具有已知数字列的动态二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22369491/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com