开发者

Using structure in C and C++

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;
开发者_Python百科

How to access elements inside profile_t??


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.


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;
}


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


Basically you can use the following format:

variable = profile_t.element
profile_t.element = ?


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;
...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜