在 C 和 C++ 中使用结构

发布于 2024-11-26 21:27:47 字数 382 浏览 0 评论 0原文

我是 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 技术交流群。

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

发布评论

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

评论(5

多彩岁月 2024-12-03 21:27:47
struct profile_t;

上述语句不会创建 profile_t 类型的对象。您需要做的是 -

struct profile_t inObj ;

然后为 profile_datagram_t 创建对象。即,

header outObj ;  // header typedef for profile_datagram_t

现在您可以访问类似的元素 -

outObj.inObj.type = 'a' ; // As an example

在 C++ 中,在为结构创建对象时,不需要 struct 关键字。


关于您的问题编辑和评论:

struct profile_t profiles[MAXPROFILES];

profilesprofile_t 类型的对象数组。要访问单个对象,只需使用 [] 运算符。即,

header obj ;
obj.profiles[0].type = 'a' ; // Example

obj.profiles[i],其中 i 可以取从 0MAXPROFILES - 1 的值,给出索引 i 处的对象。

struct profile_t;

The above statement doesn't create an object of type profile_t. What you need to do is -

struct profile_t inObj ;

Then create object for profile_datagram_t. i.e.,

header outObj ;  // header typedef for profile_datagram_t

Now you can access elements like -

outObj.inObj.type = 'a' ; // As an example

In C++, while creation of object for a structure, struct key word isn't necessary.


On your question edit and comment :

struct profile_t profiles[MAXPROFILES];

profiles is an array of objects of type profile_t. To access the individual object, just use the [] operator. i.e.,

header obj ;
obj.profiles[0].type = 'a' ; // Example

obj.profiles[i], where i can take values from 0 to MAXPROFILES - 1, gives the object at index i.

山川志 2024-12-03 21:27:47

不确定 C 中发生了什么,但在 C++ 中,除了其余的内容之外,下面声明了两种类型。

struct profile_datagram_t
{
    struct profile_t;
};

一种类型名为 profile_datagram_t,另一种类型名为 profile_datagram_t::profile_t。内部类型声明只是一个前向声明,因此您需要在后面定义类型。

struct profile_datagram_t::profile_t
{
    // ...
};

然后,您可以按如下方式使用该结构:

int main ( int, char ** )
{
    profile_datagram_t::profile_t profile;
}

Not sure what happends in C, but in C++, rest of the stuff aside, the following declares two types.

struct profile_datagram_t
{
    struct profile_t;
};

One type is named profile_datagram_t and the other is called profile_datagram_t::profile_t. The inner type declaration is just a forward declaration, so you'll need to define the type after.

struct profile_datagram_t::profile_t
{
    // ...
};

Then, you can use the struct as follows:

int main ( int, char ** )
{
    profile_datagram_t::profile_t profile;
}
寒江雪… 2024-12-03 21:27:47

一些编译器支持 C 语言的非标准扩展(我实际上很喜欢,尽管它是非标准的),称为匿名结构(或联合)。代码演示:

struct x {
  int i;
};

struct y {
  struct x;
};

int main(void)
{
    struct y;
    y.i = 1; // this accesses member i of the struct x nested in struct y
    return 0;
}

简而言之,如果您不给 struct (或 union)成员命名,则可以直接从包含的 struct 访问其成员(或联合)。这在您可能将其命名为 _ 并且必须执行 y._.i 的情况下非常有用 - 匿名结构语法要简单得多。但是,这确实意味着您必须记住两个结构体的所有成员的名称并确保它们永远不会发生冲突。

当然,这都是非标准扩展,应谨慎使用。我相信它可以在 MSVC 上运行,并且可以通过开关在 GCC 中启用。不知道其他编译器怎么样。如果您担心可移植性,请为该成员指定一个适当的名称。

编辑:根据 GCC 参考(如下),此行为将被添加到即将推出的 C1X 标准中,因此它不会长期成为非标准。我怀疑 MSVC 是否会支持 C1X,因为他们拒绝支持 C99,但至少此功能正在成为标准的一部分。

但是,上面显示的行为仅适用于 MSVC。 C1X(和没有 -fms-extensions 开关的 GCC)语法不允许未命名的 struct 成员具有名称:

struct y {
  struct {
    int i;
  };
};

int main(void) {
    struct y;
    y.i = 1; // this accesses member i of the struct x nested in struct y
    return 0;
}

各种编译器的参考(它们有不同的名称)但是相同的概念):
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:

struct x {
  int i;
};

struct y {
  struct x;
};

int main(void)
{
    struct y;
    y.i = 1; // this accesses member i of the struct x nested in struct y
    return 0;
}

In a nutshell, if you don't give the struct (or union) member a name, you can access its members directly from the containing struct (or union). This is useful in situations where you might have given it the name _, and had to do y._.i - the anonymous struct syntax is much simpler. However, it does mean that you have to remember the names of all members of both structs 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 unnamed struct member to have a name:

struct y {
  struct {
    int i;
  };
};

int main(void) {
    struct y;
    y.i = 1; // this accesses member i of the struct x nested in struct y
    return 0;
}

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

遥远的绿洲 2024-12-03 21:27:47

基本上您可以使用以下格式:

variable = profile_t.element
profile_t.element = ?

Basically you can use the following format:

variable = profile_t.element
profile_t.element = ?
如痴如狂 2024-12-03 21:27:47

编辑:在 profile_datagram_t 的声明中,struct profile_t 的正确定义应该是:

struct profile_t someProfile;

假设您有:

header profileDiagram1;
struct profile_t profile1;
profileDiagram1.someProfile = profile1;

要访问 profile_t 中的长度、类型或*数据:

profileDiagram1.someProfile.type;
profileDiagram1.someProfile.length;
...

EDIT: In your declaration of profile_datagram_t, the proper definition for struct profile_t should be:

struct profile_t someProfile;

Let's say you have:

header profileDiagram1;
struct profile_t profile1;
profileDiagram1.someProfile = profile1;

To access length, type or *data from profile_t:

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