在 C 和 C++ 中使用结构
我是 C 新手,我想知道如何访问放置在结构内部的结构内部的元素。
struct profile_t
{
unsigned char length;
unsigned char type;
unsigned char *data;
};
typedef struct profile_datagram_t
{
unsigned char src[4];
unsigned char dst[4];
unsigned char ver;
unsigned char n;
struct profile_t profiles[MAXPROFILES];
} header;
如何访问profile_t内部的元素?
I am new to C and I want to know how to access elements inside a structure which is placed inside a structure.
struct profile_t
{
unsigned char length;
unsigned char type;
unsigned char *data;
};
typedef struct profile_datagram_t
{
unsigned char src[4];
unsigned char dst[4];
unsigned char ver;
unsigned char n;
struct profile_t profiles[MAXPROFILES];
} header;
How to access elements inside profile_t??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
上述语句不会创建
profile_t
类型的对象。您需要做的是 -然后为
profile_datagram_t
创建对象。即,现在您可以访问类似的元素 -
在 C++ 中,在为结构创建对象时,不需要 struct 关键字。
关于您的问题编辑和评论:
profiles
是profile_t
类型的对象数组。要访问单个对象,只需使用[]
运算符。即,obj.profiles[i]
,其中i
可以取从 0 到 MAXPROFILES - 1 的值,给出索引i
处的对象。The above statement doesn't create an object of type
profile_t
. What you need to do is -Then create object for
profile_datagram_t
. i.e.,Now you can access elements like -
In C++, while creation of object for a structure, struct key word isn't necessary.
On your question edit and comment :
profiles
is an array of objects of typeprofile_t
. To access the individual object, just use the[]
operator. i.e.,obj.profiles[i]
, wherei
can take values from 0 to MAXPROFILES - 1, gives the object at indexi
.不确定 C 中发生了什么,但在 C++ 中,除了其余的内容之外,下面声明了两种类型。
一种类型名为
profile_datagram_t
,另一种类型名为profile_datagram_t::profile_t
。内部类型声明只是一个前向声明,因此您需要在后面定义类型。然后,您可以按如下方式使用该结构:
Not sure what happends in C, but in C++, rest of the stuff aside, the following declares two types.
One type is named
profile_datagram_t
and the other is calledprofile_datagram_t::profile_t
. The inner type declaration is just a forward declaration, so you'll need to define the type after.Then, you can use the struct as follows:
一些编译器支持 C 语言的非标准扩展(我实际上很喜欢,尽管它是非标准的),称为匿名结构(或联合)。代码演示:
简而言之,如果您不给
struct
(或union
)成员命名,则可以直接从包含的struct 访问其成员
(或联合
)。这在您可能将其命名为_
并且必须执行y._.i
的情况下非常有用 - 匿名结构语法要简单得多。但是,这确实意味着您必须记住两个结构体的所有成员的名称并确保它们永远不会发生冲突。当然,这都是非标准扩展,应谨慎使用。我相信它可以在 MSVC 上运行,并且可以通过开关在 GCC 中启用。不知道其他编译器怎么样。如果您担心可移植性,请为该成员指定一个适当的名称。
编辑:根据 GCC 参考(如下),此行为将被添加到即将推出的 C1X 标准中,因此它不会长期成为非标准。我怀疑 MSVC 是否会支持 C1X,因为他们拒绝支持 C99,但至少此功能正在成为标准的一部分。
但是,上面显示的行为仅适用于 MSVC。 C1X(和没有
-fms-extensions
开关的 GCC)语法不允许未命名的struct
成员具有名称:各种编译器的参考(它们有不同的名称)但是相同的概念):
GCC(未命名字段):http://gcc. gnu.org/onlinedocs/gcc/Unnamed-Fields.html'
MSVC(匿名结构):http://msdn. microsoft.com/en-us/library/z2cx9y4f.aspx
Some compilers support a nonstandard extension to the C language (that I actually rather like, despite it being nonstandard) called anonymous structs (or unions). Code demonstration:
In a nutshell, if you don't give the
struct
(orunion
) member a name, you can access its members directly from the containingstruct
(orunion
). This is useful in situations where you might have given it the name_
, and had to doy._.i
- the anonymous struct syntax is much simpler. However, it does mean that you have to remember the names of all members of bothstruct
s and ensure they never clash.This is all, of course, a nonstandard extension, and should be used with caution. I believe it works on MSVC and can be enabled in GCC with a switch. Don't know about any other compilers. If you're worried about portability, give the member a proper name.
EDIT: According to the GCC reference (below) this behavior is being added to the upcoming C1X standard, so it won't be nonstandard for long. I doubt MSVC will support C1X since they refuse to support C99 as it is, but at least this feature is becoming part of the standard.
However, the behavior shown above is MSVC only. The C1X (and GCC without the
-fms-extensions
switch) syntax doesn't allow the unnamedstruct
member to have a name:References for various compilers (they have different names but are the same concept):
GCC (unnamed fields): http://gcc.gnu.org/onlinedocs/gcc/Unnamed-Fields.html'
MSVC (anonymous structs): http://msdn.microsoft.com/en-us/library/z2cx9y4f.aspx
基本上您可以使用以下格式:
Basically you can use the following format:
编辑:在 profile_datagram_t 的声明中,struct profile_t 的正确定义应该是:
假设您有:
要访问 profile_t 中的长度、类型或*数据:
EDIT: In your declaration of profile_datagram_t, the proper definition for struct profile_t should be:
Let's say you have:
To access length, type or *data from profile_t: