将指针到结构的声明初始化为未命名的结构? [英] Initialize pointer-to-struct declaration to an unnamed struct?
问题描述
是否可以将结构指针的声明初始化
到一个未命名的结构?
(我只关心静态/全局变量,如果这很重要。)
我正在尝试做类似的事情:
struct st_a {
int我,j;
};
struct st_b {
int k,l;
st_a * m;
} b [] = {
1,2,{0,1},
3,4,{0 ,2}
};
可以通过声明每个子结构来完成。有一个名字,然后
指向这些,
但除了更多的工作之外,它还更难以阅读
的想法是一个层次结构。
Is it possible to have a declaration of a struct pointer initialized
to an unnamed struct?
(I''m only concerned with static/global variables, if it matters.)
I''m trying to do something like:
struct st_a {
int i, j;
};
struct st_b {
int k, l;
st_a *m;
} b[] = {
1, 2, {0, 1},
3, 4, {0, 2}
};
It could be done by declaring each "sub-struct" with a name, then
pointing to these,
but besides being more work it''s also more difficult to read when the
idea is a hierarchy.
推荐答案
Ehud Shapira写道:
Ehud Shapira wrote:
Is可以将一个结构指针的声明初始化
到一个未命名的结构吗?
(我只关心静态/全局变量,如果重要的话。)
我正在尝试做类似的事情:
struct st_a {
int i,j;
};
struct st_b {
int k,l;
st_a * m;
} b [] = {
1,2,{0,1},
3,4,{0,2}
Is it possible to have a declaration of a struct pointer initialized
to an unnamed struct?
(I''m only concerned with static/global variables, if it matters.)
I''m trying to do something like:
struct st_a {
int i, j;
};
struct st_b {
int k, l;
st_a *m;
} b[] = {
1, 2, {0, 1},
3, 4, {0, 2}
{0,1}不是指针的有效初始值设定项。
{0, 1} is not a valid initializer for a pointer.
};
可以通过声明每个子结构来完成。有一个名字,然后
指向这些,
但除了更多的工作之外,它还更难以阅读
的想法是一个层次结构。
};
It could be done by declaring each "sub-struct" with a name, then
pointing to these,
but besides being more work it''s also more difficult to read when the
idea is a hierarchy.
我不太清楚你真正想要的是什么。
It''s not quite clear to me what you actually want.
{ 0,1}不是指针的有效初始化器。
我知道。什么*可以*有效?你可以看到我正在尝试做什么样的初始化
。
{0, 1} is not a valid initializer for a pointer.
I know. What *can* be valid? You can see what sort of initialization
I''m trying to do.
Ehud Shapira写道:
Ehud Shapira wrote:
> {0,1}不是指针的有效初始值设定项。
>{0, 1} is not a valid initializer for a pointer.
我知道。什么*可以*有效?你可以看到我正在尝试做什么样的初始化
。
I know. What *can* be valid? You can see what sort of initialization
I''m trying to do.
实际上,我不能。为什么它必须是一个指针?你不能直接存储
结构吗?
Actually, I can''t. Why does it have to be a pointer? Can''t you just store a
struct directly?
这篇关于将指针到结构的声明初始化为未命名的结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!