In a book, I saw a definition like this:

static struct map_info mymap = {
.name = “mymap”,
.phys = FLASH_START_ADDR,
.size = FLASH_LENGHT,
.bankwidth = FLASH_WIDTH,
};

what does it mean..?It's not clear for me actually..

Nested structure member initialization.

#include <stdio.h>

struct test
{
   char name[20];
   int no;
};
struct pop {
   struct test p;
};
static  struct test a={
    .name="raj",
    .no=10
   };
int main()
{
  printf("\n%s   %d",a.name,a.no);
 return 0;
}

i couldn't understand the "." format...

It's one of the new features to C. You can initialize struct members by name instead of by position:

struct test
{
    int first;
    int second;
    int third;
};

struct test a = {0, 0, 1}; /* Old way */
struct test b = {.first=0, .second=0, .third=1}; /* New way */

It's useful because you don't have to initialize every member before the one you want to initialize. 0 is the default, so if I wanted to only initialize third, the old way is still the same but the new way is easier:

struct test b = {.third=1}; /* Same thing */

Designated initializers are a C99 feature. To use them you need to have a compiler that allows C99.

you don't have to initialize every member before the one you want to initialize. 0 is the default, so if I wanted to only initialize third, the old way is still the same but the new way is easier...

Wrong. Wrong. Wrong.

Structures, or any other variable, are not initialized to zero or any other value "by default" ... just because your compiler happens to do so, does not mean someone else's compiler does.

this error is probably the single-most source of broken code, and endless debugging efforts.

thanks for propagating crappy coding practice. :icon_rolleyes:


.

Wrong. Wrong. Wrong.

Structures, or any other variable, are not initialized to zero or any other value "by default" ... just because your compiler happens to do so, does not mean someone else's compiler does.

this error is probably the single-most source of broken code, and endless debugging efforts.

thanks for propagating crappy coding practice. :icon_rolleyes:


.

I'm sorry, but you are the one who is wrong. Since we're talking about a C99 feature, I'll quote the C99 standard concerning initializer lists:

If there are fewer initializers in a brace-enclosed list than there are elements or members
of an aggregate, or fewer characters in a string literal used to initialize an array of known
size than there are elements in the array, the remainder of the aggregate shall be
initialized implicitly the same as objects that have static storage duration.

If an object that has automatic storage duration is not initialized explicitly, its value is
indeterminate. If an object that has static storage duration is not initialized explicitly,
then:
— if it has pointer type, it is initialized to a null pointer;
— if it has arithmetic type, it is initialized to (positive or unsigned) zero;
— if it is an aggregate, every member is initialized (recursively) according to these rules;
— if it is a union, the first named member is initialized (recursively) according to these
rules.

If there's no initializer list, you're right about local variables. struct test a; doesn't initialize any of the members and they're indeterminate. But that's outside the topic of this discussion because this discussion is about designated initializer lists.

You lose. Thanks for playing.

struct test
{
    int first;
    int second;
    int third;
};

struct test a = {0, 0, 1}; /* Old way */
struct test b = {.first=0, .second=0, .third=1}; /* New way */

Seems like the new way is always longer if you want to initialize all the variables :P

>Designated initializers are a C99 feature. To use them you need to have a compiler that allows C99.

No, are you serious? I mean: are you kidding?

>Structures, or any other variable, are not initialized to zero or any other value "by default"
Agreed, if we're talking about C89, but if you use designated initializers (C99) then the struct's data members (which weren't explicitly initialized) are automatically initialized to zero, check it out for yourself here:
http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=215

:)

Seems like the new way is always longer if you want to initialize all the variables

Like most of the new features, there's a niche for them. The old ways are better for general use, I think.

>Structures, or any other variable, are not initialized to zero or any other value "by default"
Agreed, if we're talking about C89, but if you use designated initializers (C99) then the struct's data members (which weren't explicitly initialized) are automatically initialized to zero

It works the same way in C89. That's why int numbers[100] = {0}; sets all 100 numbers to 0 instead of just the first one and leaving the rest indeterminate.

It works the same way in C89. That's why int numbers[100] = {0}; sets all 100 numbers to 0 instead of just the first one and leaving the rest indeterminate.

Yes, but first: in C89 it's not called a designated initializer, and second: it doesn't get all those zeroes "by default", as already told by jephthah, they're automatically initialized to zero because you initialized the first element.

Yes, but first: in C89 it's not called a designated initializer

I don't see how that's relevant. The rules I quoted apply to initializer lists with or without designated initializers.

and second: it doesn't get all those zeroes "by default", as already told by jephthah, they're automatically initialized to zero because you initialized the first element.

How is that different from saying that when you initialize one element in an initializer list, the rest default to 0? Same concept, different words.

I don't see how that's relevant. The rules I quoted apply to initializer lists with or without designated initializers.


How is that different from saying that when you initialize one element in an initializer list, the rest default to 0? Same concept, different words.

Do you always want to have the last word?
Well then here's your chance, please catch it!
But remember one thing: The last word is not always the best word!

And also,
With C99, you can use an index in brackets in the initialization list to specify a particular element:


int arr[6] = {[5] = 212}; // initialize arr[5] to 212

This article has been dead for over six months. Start a new discussion instead.