NO

Author Topic: Array initialization  (Read 2355 times)

aj

  • Guest
Array initialization
« on: June 20, 2009, 04:50:37 pm »
Code: [Select]
char *txt[1] = {
[0] "asdf",
};

This works in Pelles C - should it?

Offline AlexN

  • Global Moderator
  • Member
  • *****
  • Posts: 387
    • Alex's Link Sammlung
Re: Array initialization
« Reply #1 on: June 20, 2009, 05:15:59 pm »
Code: [Select]
char *txt[1] = {
[0] "asdf",
};

This works in Pelles C - should it?

I would say YES!  ;)

You define an array of strings with one member and this member points to "asdf".   :)
best regards
 Alex ;)

nicolas.sitbon

  • Guest
Re: Array initialization
« Reply #2 on: June 20, 2009, 05:23:04 pm »
it souldn't.
at least, = operator is missing :
Code: [Select]
char *txt[1] = {
[0] = "asdf",
};
but better is
Code: [Select]
char const *txt[] = {
[0] = "asdf",
};

Let's look the standard:
Quote
6.7.8 Initialization
Syntax
1    initializer:
          assignment-expression
          { initializer-list }
          { initializer-list ,}
     initializer-list:
          designationopt initializer
          initializer-list , designationopt initializer
     designation:
          designator-list =
     designator-list:
          designator
          designator-list designator
     designator:
          [ constant-expression ]
          . identifier

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 2034
    • http://www.smorgasbordet.com
Re: Array initialization
« Reply #3 on: June 21, 2009, 01:22:12 pm »
Right now the compiler will no insist on the missing '=' (IIRC for compatibility reasons), but a warning should be issued (in standard mode at least).
/Pelle