initializing a multi-dimensional array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John

    initializing a multi-dimensional array

    I can do this

    double T[2][3] = { {1,3,4},{7,8,9} };

    but not this

    double T[2][3];

    T = { { a,b,c} , {d,e,f} }

    Why is that? Will I have to assign T[?][?] 6 times to do this?
    any better ways to do this assignment?

    Thanks,
    --j

  • John Carson

    #2
    Re: initializing a multi-dimensional array

    "John" <weekender_ny@y ahoo.comwrote in message
    news:1159582107 .164418.36450@c 28g2000cwb.goog legroups.com
    I can do this
    >
    double T[2][3] = { {1,3,4},{7,8,9} };
    >
    but not this
    >
    double T[2][3];
    >
    T = { { a,b,c} , {d,e,f} }
    >
    Why is that?
    Because the language says so.
    Will I have to assign T[?][?] 6 times to do this?
    Yes, if you can't do it when T is first declared.
    any better ways to do this assignment?
    Unlikely. If you have another array storing the required values, then you
    can memcpy from it to T.


    --
    John Carson


    Comment

    • Gianni Mariani

      #3
      Re: initializing a multi-dimensional array

      John Carson wrote:
      "John" <weekender_ny@y ahoo.comwrote in message
      news:1159582107 .164418.36450@c 28g2000cwb.goog legroups.com
      >I can do this
      >>
      >double T[2][3] = { {1,3,4},{7,8,9} };
      >>
      >but not this
      >>
      >double T[2][3];
      >>
      >T = { { a,b,c} , {d,e,f} }
      >>
      >Why is that?
      >
      Because the language says so.
      >
      >Will I have to assign T[?][?] 6 times to do this?
      >
      Yes, if you can't do it when T is first declared.
      >
      >any better ways to do this assignment?
      >
      Unlikely. If you have another array storing the required values, then you
      can memcpy from it to T.
      I strongly suggest to avoid memcpy !
      >
      >
      Alternatives are to use classes/structs.

      struct Stuff
      {
      double data[2][3];
      };

      Stuff T = { { {1,3,4},{7,8,9} } };

      void XX()
      {
      Stuff y = { { {1,3,4},{7,8,9} } };

      Stuff x = { { {y.data[0][0],3,4},{7,8,9}} };

      T = x;
      }

      Comment

      • Rolf Magnus

        #4
        Re: initializing a multi-dimensional array

        John Carson wrote:
        "John" <weekender_ny@y ahoo.comwrote in message
        news:1159582107 .164418.36450@c 28g2000cwb.goog legroups.com
        >I can do this
        >>
        >double T[2][3] = { {1,3,4},{7,8,9} };
        >>
        >but not this
        >>
        >double T[2][3];
        >>
        >T = { { a,b,c} , {d,e,f} }
        >>
        >Why is that?
        >
        Because the language says so.
        >
        >Will I have to assign T[?][?] 6 times to do this?
        >
        Yes, if you can't do it when T is first declared.
        >
        >any better ways to do this assignment?
        >
        Unlikely. If you have another array storing the required values, then you
        can memcpy from it to T.
        I'd suggest do use std::copy instead. This will work with any copyable type,
        while memcpy is only guaranteed to work with POD types.

        Comment

        • Frederick Gotham

          #5
          Re: initializing a multi-dimensional array

          John posted:
          I can do this
          >
          double T[2][3] = { {1,3,4},{7,8,9} };
          >
          but not this
          >
          double T[2][3];
          >
          T = { { a,b,c} , {d,e,f} }
          >
          Why is that? Will I have to assign T[?][?] 6 times to do this?
          any better ways to do this assignment?
          I've cooked up some code in the last hour for assigning to an aggregate
          using initialiser syntax. It's not bullet-proof, but it's a good start.

          NB:
          (1) It uses variadic macros.
          (2) It assumes that we can treat an array as if it were a struct
          containing a sole member which is an array (not an unfair assumption if you
          ask me).
          (3) It will malfunciton if the type name contains commas.


          template<class Tstruct TypeProcurer {typedef T Type;};

          #define ASSIGN_AGG(arr, TYPE,...) \
          do { \
          \
          struct SoleMem { \
          \
          TypeProcurer<TY PE>::Type sole_member; \
          \
          static SoleMem GetLit() \
          { \
          SoleMem const lit = { __VA_ARGS__ }; \
          return lit; \
          } \
          \
          }; \
          \
          reinterpret_cas t<SoleMem&>(arr ) = \
          SoleMem::GetLit (); \
          } while(0)


          /* Just some samples to test it out: */

          int Func1( double (&(*)(char*))[5] ) {return 5;}
          int Func2( double (&(*)(char*))[5] ) {return 5;}
          int Func3( double (&(*)(char*))[5] ) {return 5;}

          int main()
          {
          double arr[2][3];
          ASSIGN_AGG(arr, double[2][3], { {2.3,8.0,5.4}, {7.6,2.8,9.1} } );

          /* Now let's try a more complicated one: */

          int (*func_ptr_arr[3])( double (&(*)(char*))[5] );

          ASSIGN_AGG(func _ptr_arr,int (*[3])( double (&(*)(char*))[5] ),
          {Func1,Func2,Fu nc3});
          }

          --

          Frederick Gotham

          Comment

          Working...