Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: new operator in conjunction with Multi dimensional array

Reply
Thread Tools

Re: new operator in conjunction with Multi dimensional array

 
 
John Harrison
Guest
Posts: n/a
 
      07-13-2003

"muser" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> I'm having trouble with assigning a multi dimensional character array
> the new operator.
>
> I've tried this: char temp1[12][max] = new char temp1[12][max];


This is not possible in C++, and your syntax is quite wrong as well. Suggest
you read the FAQ on multidimensional arrays

http://www.parashift.com/c++-faq-lit...html#faq-16.15

But its more complex than your realise, perhaps you need to get a better
grounding in C++ concepts before trying this. An alternative would be to
used a fixed size multiple dimensional array.

>
> the following error is shown:
> C:\Program Files\Microsoft Visual
> Studio\MyProjects\Valid\Zenith124\Zenith.cpp(102) : error C2440:
> 'initializing' : cannot convert from 'char *' to 'char [12][104]'
>
> Before main I have initialised temp1 as shown: char temp1[12][max].
>
> Also can a multi dimensional array contain members of a structure like
> so?
>
> char temp1[12][max] = {
> rec.customer_code[6],
> rec.customer_address[61],
> rec.customer_name[21],
> rec.record_type,
> rec.credit_limit[8],
> rec.customer_balance[10],
> };


Again its hard to understand what you are attempting here.

You can have a multidimensional array of structures of course, but I think
maybe you are asking how to assign members of a structure to a
multidimensional array.

The first thing to realise is that you cannot assign arrays at all in C++.
What you can to is copy the elements of the array one by one. In the case of
character arrays that are strings you can use the handy strcpy function to
do this.

strcpy(temp1[0], rec.customer_code);
strcpy(temp1[1], rec.customer_name);
strcpy(temp1[3], rec.credit_limit);

This may not work because you haven't supplied the definition of rec, so I
can't be sure the above code is right.

I seem to remember you posting this structure before, a while back. Is that
right? I seem to remember then you were very confused about what should be
an array and what should not, you had made a lot of things arrays when in
fact they shouldn't have been. Have you cleared that confusion up yet? The
above code shows you are still confused about the syntax to handle arrays.
Apologies if I've confused you with someone else.


>
> the structure members have nothing in them but will do later in the
> program.
> Thank you in advance for your reply.


john


 
Reply With Quote
 
 
 
 
John Ericson
Guest
Posts: n/a
 
      07-13-2003
"John Harrison" <(E-Mail Removed)> wrote in
message news:besdpq$8ge07$(E-Mail Removed)-berlin.de...
>
> "muser" <(E-Mail Removed)> wrote in message

<snip>
> > Studio\MyProjects\Valid\Zenith124\Zenith.cpp(102) :

error C2440:

<snip>

muser> > char temp1[12][max] = {
muser> > rec.customer_code[6],
muser> > rec.customer_address[61],
muser> > rec.customer_name[21],
muser> > rec.record_type,
muser> > rec.credit_limit[8],
muser> > rec.customer_balance[10],
muser> > };

<snip>

> I seem to remember you posting this structure before, a

while back.

<snip>

You're probably remembering back to your response to muser
back in 21Dec2002. I've only been reading the news group
since around that time, so it may have been something
earlier I missed... Best Regards, John E.

john> Here's my intepretation of the correct structore
declaration (only an
john> interpretation because I don't have the details of the
assignment you are
john> doing).

john> struct file {
john> char customer_name[20];
john> char customer_address[60];
john> char record_type;
john> int credit_limit;
john> int customer_balance;
john> int customer_code;
john> };

john> Hope this is constructive.



 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      07-14-2003

"John Ericson" <(E-Mail Removed)> wrote in message
news:LHlQa.780$(E-Mail Removed) .com...
> "John Harrison" <(E-Mail Removed)> wrote in
> message news:besdpq$8ge07$(E-Mail Removed)-berlin.de...
> >
> > "muser" <(E-Mail Removed)> wrote in message

> <snip>
> > > Studio\MyProjects\Valid\Zenith124\Zenith.cpp(102) :

> error C2440:
>
> <snip>
>
> muser> > char temp1[12][max] = {
> muser> > rec.customer_code[6],
> muser> > rec.customer_address[61],
> muser> > rec.customer_name[21],
> muser> > rec.record_type,
> muser> > rec.credit_limit[8],
> muser> > rec.customer_balance[10],
> muser> > };
>
> <snip>
>
> > I seem to remember you posting this structure before, a

> while back.
>
> <snip>
>
> You're probably remembering back to your response to muser
> back in 21Dec2002. I've only been reading the news group
> since around that time, so it may have been something
> earlier I missed... Best Regards, John E.
>
> john> Here's my intepretation of the correct structore
> declaration (only an
> john> interpretation because I don't have the details of the
> assignment you are
> john> doing).
>
> john> struct file {
> john> char customer_name[20];
> john> char customer_address[60];
> john> char record_type;
> john> int credit_limit;
> john> int customer_balance;
> john> int customer_code;
> john> };
>
> john> Hope this is constructive.
>


Well if that is right then there is a problem. You can certainly copy
customer_name, customer_address to the multidimensional char array because
both are strings. As a single char record_type could be copied as well. But
there is a bit of a problem with credit_limit and the rest because they are
integers and would need to be converted to character data before being
copied. Frankly I doubt that is a good idea but if you really wanted to do
it, it would be something like this

sprintf(temp1[4], "%d", rec.credit_limit);

The sprintf function performs the conversion from int to char array.

But as I said this doesn't really seem like a good idea, perhaps muser could
say what s/he is trying to acheive.

john


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
To convert a one dimensional array into a two dimensional array using C amrutha0303 Software 0 08-03-2010 10:02 PM
multi-dimensional arrays to 2-dimensional arrays Wirianto Djunaidi Ruby 2 04-29-2008 07:31 AM
acessing multi-dimensional array created via new TrustyTif C++ 6 06-09-2004 09:50 PM
How do copy Strings from a single dimensional array to double dimensional array Venkat C++ 4 12-05-2003 09:23 AM
Re: new operator in conjunction with Multi dimensional array Samuele Armondi C++ 0 07-13-2003 08:02 PM



Advertisments