尝试在 C 中创建数组的数组
所以我有一堆布尔数组,我想将它们放入一个数组中以便于访问,但由于某种原因这不太有效。
我的数组如下所示:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您想要存储已创建的数组而不复制每个元素时,不能声明数组的数组,但可以声明指针数组:
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:
我的 C-fu 有点弱,但你必须这样做:
基本上只有在你初始化它时才应该使用
int variable[]
,否则它不会工作。像这样设置数组位置也
不起作用,因为您试图将第一个元素设置为数组。
My C-fu is kinda weak, but you have to do this:
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
doesn't work because you are trying to set the first element to be the array.