tags:

views:

258

answers:

3

While reading a code I came across, the following definition and initialization of a struct:

// header file
struct foo{
char* name;
int value;
};

//Implementation file
struct foo fooElmnt __foo;
// some code using fooElmnt
struct foo fooElmnt __foo = {
    .name = "NAME";
    .value = some_value;
}

What does this mean in C and how is it different from usual declarations?

+5  A: 

It's called designated initialization,

In a structure initializer, specify the name of a field to initialize with .fieldname = before the element value. For example, given the following structure,

 struct point { int x, y; };

the following initialization

 struct point p = { .y = yvalue, .x = xvalue };

is equivalent to

 struct point p = { xvalue, yvalue };

If you read on, it explains that .fieldname is called a designator.

UPDATE: I'm no C99 expert, but I couldn't compile the code. Here's the changes I had to make:

// header file
struct foo{
char* name;
int value;
};

//Implementation file
//struct foo fooElmnt __foo;
// some code using fooElmnt
struct foo fooElmnt  = {
    .name = "NAME",
    .value = 123
};

Were you able to compile it? I used TCC.

Jacob
aah thanks for that:), but how do you explain `struct foo elmnt __foo` declaration
Neeraj
@Neeraj: regarding the `struct foo fooElmnt __foo` sequence - I'd guess that `__foo` (or `fooElmnt`) is a macro that is either empty or has a compiler specific attribute for the structure/variable.
Michael Burr
@Jacob - it should be mentioned that this is a C99 feature that isn't in C++ (and unfortunately aren't slated for the upcoming C++0x standard as far as I know). That's one reason you don't see it used very often.
Michael Burr
@Michael: Yeah, I couldn't figure out the `__foo` - your explanation makes the most sense!
Jacob
@Michael, thanks i also thought the same but i was not sure ;)@jacob, I was also unable to compile the code independently, so it must be a macro or attribute. time for some grep'ing. :P
Neeraj
+2  A: 

Those are designated initializers, introduced in c99. You can read more here

Without them, you'd use

struct foo fooElmnt __foo = {
    "NAME",
    some_value
};

While in this case it doesn't matter much - other than the c99 way is more verbose, and its easier to read which element is initialized to what.

It does help if your struct has a lot of members and you only need to initialize a few of them to something other than zero.

nos
can you explain `__foo` in the struct definition
Neeraj
In the code show, it's a syntax error, you'll have to dig through more code/header files to see if it's a #define somewhere.
nos
+2  A: 

This is a designated initialization. This also initializing the fields by their name, which is more readable than anomynous initialization when the structures are getting large. This has been introduced by the C99 standard.

philippe
can you explain `__foo` in struct definition.
Neeraj
Not without the provided snippet, fooElmnt or __foo could be defines used to document something, or provide a hint to the compiler or a code generator. Do you have an example that compiles ?
philippe