尝试在 C 中创建数组的数组

发布于 2025-01-07 17:16:58 字数 498 浏览 0 评论 0原文

所以我有一堆布尔数组,我想将它们放入一个数组中以便于访问,但由于某种原因这不太有效。

我的数组如下所示:

boolean l1_000[8] = {1,0,0,0,0,0,0,0};

我声明我的数组数组:

boolean level1[8];

然后我想我这两个中的任何一个都可以(第一个只是直接声明这些数组,我将它们设置在大数组上):

level1[0] = {1,0,0,0,0,0,0,0};
level1[0] = l1_000;

我也尝试了 level1[8][],但这也不起作用。那么我在这里做错了什么?我该怎么做?

编辑:所以我设法通过将数组声明为 boolean *level1[8] 来做到这一点,但这只允许我执行 level1[0] = l1_000。有什么办法可以做到 level1[0] = {1,0,0,0,0,0,0,0} 吗?

So I have a bunch of boolean arrays that I would like to put into a single array for easier accessing, but for some reason this doesn't quite work.

My arrays look like this:

boolean l1_000[8] = {1,0,0,0,0,0,0,0};

I declare my array of arrays with:

boolean level1[8];

And then I figured I could either of these two (first of which just declaring these arrays directly where I set them on the big array):

level1[0] = {1,0,0,0,0,0,0,0};
level1[0] = l1_000;

I also tried level1[8][], but that didn't work either. So what am I doing wrong here? How would I do this?

EDIT: So I managed to do this by declaring the array as boolean *level1[8], but that only allows me to do level1[0] = l1_000. Is there any way I can do level1[0] = {1,0,0,0,0,0,0,0}?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空心空情空意 2025-01-14 17:16:58

当您想要存储已创建的数组而不复制每个元素时,不能声明数组的数组,但可以声明指针数组:

boolean* level1[] = {
    l1_000, // the array name decays to a pointer to the first element
    l2_000,
    // etc
};

You can't declare an array of arrays when you want to store arrays that are already created without copying each element, but you can declare an array of pointers:

boolean* level1[] = {
    l1_000, // the array name decays to a pointer to the first element
    l2_000,
    // etc
};
下壹個目標 2025-01-14 17:16:58

我的 C-fu 有点弱,但你必须这样做:

int l1_000[8] = {1,0,0,0,0,0,0,0}; // one row
int level1[8][8];// 8 high, 8 wide
memcpy(level1[0], l1_000, sizeof(int)*8); // size of int * number of memory elements

基本上只有在你初始化它时才应该使用 int variable[] ,否则它不会工作。

像这样设置数组位置也

level1[0] = l1_000;

不起作用,因为您试图将第一个元素设置为数组。

My C-fu is kinda weak, but you have to do this:

int l1_000[8] = {1,0,0,0,0,0,0,0}; // one row
int level1[8][8];// 8 high, 8 wide
memcpy(level1[0], l1_000, sizeof(int)*8); // size of int * number of memory elements

basically doing int variable[] will should only be used when you are initializing it right then and there other wise it won't work.

also setting the array position like this

level1[0] = l1_000;

doesn't work because you are trying to set the first element to be the array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文