why can not assign value to an array variable of a structure?

Discussion in 'C++' started by baumann@pan, May 9, 2005.

  1. baumann@pan

    baumann@pan Guest

    hi all,


    #include "stdafx.h"

    struct a_{
    int a;
    int b;
    };

    struct a_ a1 ,a2, a3, a4, a5, a6;
    #if 0
    struct a_ m[6] = { a1,a2,a3,a4,a5,a6};
    #endif
    int a[6] = { 1, 2, 3, 4, 5, 6};

    int main(int argc, char* argv[])
    {
    printf("Hello World!\n");
    return 0;
    }


    the above code tells me struct a_ m[6] ={ }; statement is wrong,

    but why int a[6] ={}; is ok?

    i know if i write struct a_ m[6] = {
    {a1.a,a1.b},{a2.a,a2.b},{a3.a,a3.b},{
    a4.a,a4.b},{a5.a,a5.b}}; it will be ok, i have not test the idea.
     
    baumann@pan, May 9, 2005
    #1
    1. Advertising

  2. baumann@pan

    Arun Guest

    Re: why can not assign value to an array variable of a structure?

    > struct a_ m[6] = { a1,a2,a3,a4,a5,a6};

    initialization elements must be constants which in this case (a1..a6)
    are not.

    -ASJ
     
    Arun, May 9, 2005
    #2
    1. Advertising

  3. "baumann@pan" <> wrote in message
    news:...
    | hi all,
    |
    |
    | #include "stdafx.h"
    |
    | struct a_{
    | int a;
    | int b;
    | };
    |
    | struct a_ a1 ,a2, a3, a4, a5, a6;
    | #if 0
    | struct a_ m[6] = { a1,a2,a3,a4,a5,a6};
    | #endif
    | int a[6] = { 1, 2, 3, 4, 5, 6};
    |
    | int main(int argc, char* argv[])
    | {
    | printf("Hello World!\n");
    | return 0;
    | }
    |
    |
    | the above code tells me struct a_ m[6] ={ }; statement is wrong,
    |
    | but why int a[6] ={}; is ok?

    It might be a compiler bug?
    It works fine with MinGW.

    | i know if i write struct a_ m[6] = {
    | {a1.a,a1.b},{a2.a,a2.b},{a3.a,a3.b},{
    | a4.a,a4.b},{a5.a,a5.b}}; it will be ok, i have not test the idea.

    The following should work:
    a_ m[6] = { {5,10}, {15,20}, {25,30}, {35,40}, {45,50}, {55,60} };

    Btw, you don't need the 'struct' keyword in the declaration
    in C++.

    Cheers,
    Chris Val
     
    Chris \( Val \), May 9, 2005
    #3
  4. baumann@pan

    John Carson Guest

    "baumann@pan" <> wrote in message
    news:
    > hi all,
    >
    >
    > #include "stdafx.h"
    >
    > struct a_{
    > int a;
    > int b;
    > };
    >
    > struct a_ a1 ,a2, a3, a4, a5, a6;
    > #if 0
    > struct a_ m[6] = { a1,a2,a3,a4,a5,a6};
    > #endif
    > int a[6] = { 1, 2, 3, 4, 5, 6};
    >
    > int main(int argc, char* argv[])
    > {
    > printf("Hello World!\n");
    > return 0;
    > }
    >
    >
    > the above code tells me struct a_ m[6] ={ }; statement is wrong,
    >
    > but why int a[6] ={}; is ok?
    >
    > i know if i write struct a_ m[6] = {
    > {a1.a,a1.b},{a2.a,a2.b},{a3.a,a3.b},{
    > a4.a,a4.b},{a5.a,a5.b}}; it will be ok, i have not test the idea.



    It compiles without complaint on VC++ 7.1 and Comeau.

    --
    John Carson
     
    John Carson, May 9, 2005
    #4
  5. baumann@pan

    baumann@pan Guest

    Re: why can not assign value to an array variable of a structure?

    i am using VC++ 6, not the one u use.

    does the standard of C99 /C++ guide what we should do under this case?
     
    baumann@pan, May 9, 2005
    #5
  6. baumann@pan

    baumann@pan Guest

    Re: why can not assign value to an array variable of a structure?

    i tried with vs.net 2003 studio, it works, but VC++ 6.0 really fails to
    compile the code.
     
    baumann@pan, May 10, 2005
    #6
  7. baumann@pan

    Jack Klein Guest

    Re: why can not assign value to an array variable of a structure?

    On 9 May 2005 02:55:22 -0700, "baumann@pan" <>
    wrote in comp.lang.c++:

    > i am using VC++ 6, not the one u use.
    >
    > does the standard of C99 /C++ guide what we should do under this case?


    The standard of C99 is not relevant to C++. The C++ standard is based
    in part on the C standard as of 1995.

    As for the C++ standard, it suggests that you use a compiler that
    conforms to the C++ standard. Microsoft Visual C++ 6.0 does not.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, May 10, 2005
    #7
  8. baumann@pan

    baumann@pan Guest

    Re: why can not assign value to an array variable of a structure?

    The standard of C99 is not relevant to C++. The C++ standard is based
    in part on the C standard as of 1995.

    As for the C++ standard, it suggests that you use a compiler that
    conforms to the C++ standard. Microsoft Visual C++ 6.0 does not.

    according to your view, if the compiler conforms to the C++ standard,
    the code should work? right? thanks
     
    baumann@pan, May 10, 2005
    #8
  9. baumann@pan

    Achintya Guest

    Re: why can not assign value to an array variable of a structure?

    Hi,

    As mentioned by Mr. Arun also, this kind of array initialization
    requires constant values.
    Since the struct are already initialized to zero( static variable
    initilization) in the previous statement i.e

    struct a_ a1 ,a2, a3, a4, a5, a6;

    ...those values are considered for initialization in the array of
    structs if you use...

    struct a_ m[6] =
    {{a1.a,a1.b},{a2.a,a2.b},{a3.a,a3.b},{a4.a,a4.b},{a5.a,a5.b}};

    ....and if you use....

    a_ m[6] = {10,20,30,40,50,60};

    ....the initialization is m[0].a=10, m[0].b=20,
    m[1].a=30....m[4].a=0...etc...

    I have tried on VC++ 6.0.
     
    Achintya, May 10, 2005
    #9
    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. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,949
    Smokey Grindel
    Dec 2, 2006
  2. ed
    Replies:
    3
    Views:
    341
    James Mills
    Oct 26, 2008
  3. Jase Schick
    Replies:
    11
    Views:
    588
    Geoff
    Aug 5, 2010
  4. Matt
    Replies:
    2
    Views:
    861
    Anthony Judd
    Sep 25, 2004
  5. Harry Stangel

    Can I assign an external value to a JS variable?

    Harry Stangel, Jun 6, 2004, in forum: Javascript
    Replies:
    2
    Views:
    101
    Harry Stangel
    Jun 8, 2004
Loading...

Share This Page