NO

Author Topic: Array initialization  (Read 3689 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?

Online AlexN

  • Global Moderator
  • Member
  • *****
  • Posts: 394
    • 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: 2266
    • 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