开发者

Initializing structure by named members

I'm trying to wrap my head around the syntax provided in http://publib.boulder.ibm.com/infocenter/lnxpcomp/v7v91/index.jsp?topic=%2Fcom.ibm.vacpp7l.doc%2Flanguage%2Fref%2Fclrc03strin.htm :

struct
{
    int a[5], b;
} game[] =
{
    [0].a = { 1 },
    [1].a[0] = 2
};

Ideally, I'd find some way to do the following:

struct
{
    开发者_运维技巧int a, b;
} foo =
{
    .a = 4,
    .b = 5
};

My reason for wanting to have a by-name initialization of a structure is that my own stucture has many members, so I want better clarity. I shouldn't just initialize them in separate statements because this is a performance-sensitive embedded application where the members are actually bitfields, so a single struct init generates fewer instructions than multiple member initializations.

Which C standard allows for by-name member init like that?


It's not immediately clear what you're asking, but with a c99 compiler, your first attempt could be written as

struct
{
    int a[5], b;
} game[] =
{
    [0] = {.a = { 1 }},
    [1] = {.a[0] = 2}

};


I'm doing something similar in an embedded app under C99. I have a variable called phaseInstill that is "assigned" to a struct:

    phaseInstill = (PhaseVolumeStatus)
    {
        .complete = false,
        .ticksInstilled = 0,
        .volumeInstilled = 0,
        .volumeRemaining = instillVolume
    };

Where PhaseVolumeStatus is defined as:

typedef struct
{
    Value volumeRemaining;              /*!> ml */  
    Value volumeInstilled;              /*!> ml */
    Value ticksInstilled;               /*!> ticks */

    bool complete;
} PhaseVolumeStatus;

I don't have a bitfield example handy, but I don't recall it working any differently in any other of my uses.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜