struct initialization

Discussion in 'C Programming' started by John Smith, Oct 5, 2005.

  1. John Smith

    John Smith Guest

    Is this a no-no? My compiler doesn't like it.

    typedef struct cplex {
    double real;
    double imag;
    } cplex;

    int main(void)
    {
    cplex a;

    a = {.5, 14.134725};

    ...

    return 0;
    }

    Compiler has no problem when declaration
    and initialization are combined:

    cplex a = {.5, 14.134725};
     
    John Smith, Oct 5, 2005
    #1
    1. Advertising

  2. John Smith wrote:
    > Is this a no-no? My compiler doesn't like it.


    It is a no-no; your compiler ought not like it.

    > typedef struct cplex {
    > double real;
    > double imag;
    > } cplex;
    >
    > int main(void)
    > {
    > cplex a;
    >
    > a = {.5, 14.134725};
    >
    > ...
    >
    > return 0;
    > }


    /* WARNING: This will not work with C89 (C90). If you have a C99
    compiler (or gcc) then it is also pointless, since you will have complex
    types already. */

    typedef struct cplex
    {
    double real;
    double imag;
    } cplex;

    int main(void)
    {
    cplex a;
    a = (cplex) { .5, 14.134725};
    return 0;
    }
     
    Martin Ambuhl, Oct 5, 2005
    #2
    1. Advertising

  3. John Smith

    Skarmander Guest

    John Smith wrote:
    > Is this a no-no? My compiler doesn't like it.
    >
    > typedef struct cplex {
    > double real;
    > double imag;
    > } cplex;
    >
    > int main(void)
    > {
    > cplex a;
    >
    > a = {.5, 14.134725};
    >

    <snip>
    Struct literals are not supported in C90. In C99 they are, but you must
    use special syntax:

    a = (cplex) {.5, 14.134725};

    But then, in C99 you have built-in complex types and you don't need cplex.

    S.
     
    Skarmander, Oct 5, 2005
    #3
  4. "John Smith" <> wrote in message
    news:enX0f.90671$oW2.31245@pd7tw1no...
    > Is this a no-no? My compiler doesn't like it.
    >
    > typedef struct cplex {
    > double real;
    > double imag;
    > } cplex;
    >
    > int main(void)
    > {
    > cplex a;
    >
    > a = {.5, 14.134725};
    >
    > ...
    >
    > return 0;
    > }
    >
    > Compiler has no problem when declaration
    > and initialization are combined:
    >
    > cplex a = {.5, 14.134725};


    The following compiles with gcc w/o warnings (I use -Wall) and seems to
    work, but I'm not sure of its correctness according to the C standard:

    #include <stdio.h>

    typedef struct ts {
    long a;
    short b;
    } ts;

    ts s1 = {0, 1};
    ts s2;

    int main()
    {
    s2 = (struct ts){2, 3};
    printf ("%ld,%d\n", s1.a, (int)s1.b);
    printf ("%ld,%d\n", s2.a, (int)s2.b);
    return 0;
    }

    Old Borland C/C++ compiler for DOS doesn't like it though. I can't find a
    place in the standard saying something about these things. I've seen
    something before, but can't remember what and where...

    Alex
     
    Alexei A. Frounze, Oct 5, 2005
    #4
  5. John Smith

    Skarmander Guest

    Skarmander wrote:
    > John Smith wrote:
    >
    >> Is this a no-no? My compiler doesn't like it.
    >>
    >> typedef struct cplex {
    >> double real;
    >> double imag;
    >> } cplex;
    >>
    >> int main(void)
    >> {
    >> cplex a;
    >>
    >> a = {.5, 14.134725};
    >>

    > <snip>
    > Struct literals are not supported in C90.


    Interestingly, gcc requires -pedantic to merely warn about this in C90
    mode. But then, relying on your compiler to judge whether code is
    correct is a bad idea anyway. :)

    S.
     
    Skarmander, Oct 5, 2005
    #5
  6. John Smith

    Mike Wahler Guest

    "John Smith" <> wrote in message
    news:enX0f.90671$oW2.31245@pd7tw1no...
    > Is this a no-no?


    Yes-yes. :)

    > My compiler doesn't like it.


    It shouldn't.

    >
    > typedef struct cplex {
    > double real;
    > double imag;
    > } cplex;
    >
    > int main(void)
    > {
    > cplex a;
    >
    > a = {.5, 14.134725};


    This is not initialization, but (an attempt at)
    assignment. Assignment is not initialization.

    > return 0;
    > }
    >
    > Compiler has no problem when declaration
    > and initialization are combined:
    >
    > cplex a = {.5, 14.134725};


    Right. This is initialization.

    I believe C99 supports what you're trying to do,
    but I'm not familiar with the details.

    -Mike
     
    Mike Wahler, Oct 6, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JKop
    Replies:
    10
    Views:
    992
  2. Chris Fogelklou
    Replies:
    36
    Views:
    1,441
    Chris Fogelklou
    Apr 20, 2004
  3. Matthias Kaeppler
    Replies:
    2
    Views:
    470
    Victor Bazarov
    Jul 18, 2005
  4. Replies:
    6
    Views:
    483
    Ron Natalie
    Dec 11, 2005
  5. Replies:
    2
    Views:
    769
    David Harmon
    Sep 20, 2006
Loading...

Share This Page