473,382 Members | 1,376 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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

Sep 30 '06 #1
4 2058
"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

50
by: Dan Perl | last post by:
There is something with initializing mutable class attributes that I am struggling with. I'll use an example to explain: class Father: attr1=None # this is OK attr2= # this is wrong...
14
by: Avi Uziel | last post by:
Hi All, I'm writing a Windows DLL which contain some utility classes. One of my classes is Singleton, therefore contain some static members. I'm using VC6 and linking to the DLL statically. ...
6
by: Andrew V. Romero | last post by:
First off, I am mostly familier with PHP but am trying to make a multi-step javascript program. For an example, I would like to run different sections of the script depending on how many times the...
7
by: Zaharije Pasalic | last post by:
I tried to implement multi-data list with a reference instead of pointer to data, but code compiled with gcc perform "segmentation fault". Generated code from other compilers: Borland C++ 5.0 and...
9
by: trint | last post by:
Ok, I have a thread that I start when user clicks to start: ThreadStart myThreadDelegate = new ThreadStart ThreadFunction1.getOneAtATime); Thread thr1 = new Thread(myThreadDelegate);...
8
by: SM | last post by:
I've always wonder if there is diference when declaring and initializing a varible inside/outside a loop. What's a better practice? Declaring and initializing variables inside a loop routine,...
0
by: wbsmith | last post by:
hello all, i am using python (2.5) with the PIL (latest release), and i am trying to read in these 3-d tiff images we have. basically, i am following some example code that iterates through...
38
by: junky_fellow | last post by:
Guys, I was just looking at some code where to initialize an integer with all F's following statement is used; unsigned int i = -1; I want to know if this is the right way of doing the...
10
by: Jason Doucette | last post by:
Situation: I have a simple struct that, say, holds a color (R, G, and B). I created my own constructors to ease its creation. As a result, I lose the default constructor. I dislike this, but...
9
by: Mark2012 | last post by:
Hello, I am new to programming with python. I am using the tutorial, "Byte of Python" and am on p. 82. I have come across something very unusual by accident and I was wondering if anybody here could...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.