By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,279 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

initializing a multi-dimensional array

P: n/a
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

Sep 30 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"John" <we**********@yahoo.comwrote in message
news:11*********************@c28g2000cwb.googlegro ups.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
Sep 30 '06 #2

P: n/a
John Carson wrote:
"John" <we**********@yahoo.comwrote in message
news:11*********************@c28g2000cwb.googlegro ups.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;
}
Sep 30 '06 #3

P: n/a
John Carson wrote:
"John" <we**********@yahoo.comwrote in message
news:11*********************@c28g2000cwb.googlegro ups.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.

Sep 30 '06 #4

P: n/a
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<TYPE>::Type sole_member; \
\
static SoleMem GetLit() \
{ \
SoleMem const lit = { __VA_ARGS__ }; \
return lit; \
} \
\
}; \
\
reinterpret_cast<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,Func3});
}

--

Frederick Gotham
Sep 30 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.