在一行中初始化一个结构体数组? (在C中)

发布于 2024-11-07 05:43:37 字数 619 浏览 4 评论 0原文

我有一个像这样的二维结构数组,

MapStruct myMap[50][50];

所以我们可以像这样初始化,

myMap[0][0].left = 0;
myMap[0][0].up = 1;
myMap[0][0].right = 5;

我知道我也可以使用下面的示例,

MapStruct myMap[50][50] = { {0,1,5}, {2,3,7}, {9,11,8} ... };

但问题是这个 50x50 结构中有明显的空点。例如,从 [30][40] 到 [40][50] 可能是空的 这里和那里的其他一些点都是空的,所以使用上面的括号表示法,我必须为这些空点留下像这样的空括号 {},{},{} 。

现在我的问题是有没有办法像下面这样初始化?

myMap[0][0] = {0, 1, 5}; // gives a syntax error

每点节省两行,我对此感到非常满意。

Ps:我使用索引 myMap[x][y] 作为像字典对象一样的键,所以我不能仅仅删除中间的那些空索引,因为这会改变索引。

I have a 2d array of structs like this,

MapStruct myMap[50][50];

So we can initialize like this,

myMap[0][0].left = 0;
myMap[0][0].up = 1;
myMap[0][0].right = 5;

I know that I can also use the below example,

MapStruct myMap[50][50] = { {0,1,5}, {2,3,7}, {9,11,8} ... };

But the problem is that there are significant empty spots in this 50x50 structure. So for example maybe from [30][40] up to [40][50] is empty
and some other points here and there are empty so with the above bracket notation i have to leave empty brackets like this {},{},{} for those empty spots.

Now my question is is there a way to initialize the like below?

myMap[0][0] = {0, 1, 5}; // gives a syntax error

Saves two lines per point I'd be pretty happy with that.

Ps: I'm using the indices myMap[x][y] as keys like a dictionary object so I can't just get rid of those empty ones in the middle because that changes the indices.

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

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

发布评论

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

评论(5

遮了一弯 2024-11-14 05:43:37

C99 允许

myMap[0][0] = (MapStruct){0, 1, 5};

如果您仅限于 C90,则可以使用辅助函数。

mypMap[4][2] = makeStruct(3, 6, 9);

但请注意,

MapStruct myMap[50][50];

如果没有初始化程序,函数中不会用 0 值初始化数组,因此您必须使用

MapStruct myMap[50][50] = {0};

并且还要注意,人们可能想知道在堆栈上分配这么大的数组是否明智。

C99 allows

myMap[0][0] = (MapStruct){0, 1, 5};

If you are restricted to C90, you can use an helper function.

mypMap[4][2] = makeStruct(3, 6, 9);

But note that

MapStruct myMap[50][50];

in a function won't initialize the array with 0 values if there are no initializer, so you'll have to use

MapStruct myMap[50][50] = {0};

And also note that one may wonder if it is wize to allocate such big arrays on the stack.

醉生梦死 2024-11-14 05:43:37

如果你尝试使用:

myMap[0][0] = (MapStruct) {.left = 0, .up = 1, .right = 5};

或者

myMap[0][0] = (MapStruct) {0, 1, 5};

If you try with :

myMap[0][0] = (MapStruct) {.left = 0, .up = 1, .right = 5};

Or

myMap[0][0] = (MapStruct) {0, 1, 5};
我们只是彼此的过ke 2024-11-14 05:43:37

C99 允许这样初始化:

MapStruct myMap[50][50] = {
    [ 0][ 5] = { /* ... */ },
    [10][20] = { /* ... */ },
    /* ... */
};

或者,您可以通过赋值来设置值,如下所示:

MapStruct myMap[50][50];
/* ... */
myMap[ 0][ 5] = (MapStruct){ /* ... */ };
myMap[10][20] = (MapStruct){ /* ... */ };
/* ... */

请注意,第二个方法中的语法不是转换。它是 C99 引入的表示法,虽然看起来与强制转换相同,但用于编写聚合类型的文字。

C99 allows initialization like this:

MapStruct myMap[50][50] = {
    [ 0][ 5] = { /* ... */ },
    [10][20] = { /* ... */ },
    /* ... */
};

Or, you can set up the values by assignment, like this:

MapStruct myMap[50][50];
/* ... */
myMap[ 0][ 5] = (MapStruct){ /* ... */ };
myMap[10][20] = (MapStruct){ /* ... */ };
/* ... */

Be aware that the syntax in the second method is not casting. It is notation introduced by C99 that, although it looks the same as a cast, is used to write literals of aggregate types.

注定孤独终老 2024-11-14 05:43:37

如果您使用的是 C99,请查找复合文字。

If you're using C99, look up compound literal.

掩饰不了的爱 2024-11-14 05:43:37

这将为您提供堆上而不是堆栈上的数据,但您想要做的事情可以通过 中的 calloc 来实现。 calloc 将分配内存空间并将其初始化为零。

MyStruct * myMap = calloc( 50, 50 * sizeof(MyStruct));

This will give you data on the heap rather than the stack but what you want to do could be achieved with calloc from <stdlib.h>. calloc will allocate the memory space and it set initialize it to zero.

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