Re: new operator in conjunction with Multi dimensional array

Discussion in 'C++' started by John Harrison, Jul 13, 2003.

  1. "muser" <> wrote in message
    news:...
    > 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-lite/freestore-mgmt.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
     
    John Harrison, Jul 13, 2003
    #1
    1. Advertising

  2. John Harrison

    John Ericson Guest

    "John Harrison" <> wrote in
    message news:besdpq$8ge07$-berlin.de...
    >
    > "muser" <> 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.
     
    John Ericson, Jul 14, 2003
    #2
    1. Advertising

  3. "John Ericson" <> wrote in message
    news:LHlQa.780$...
    > "John Harrison" <> wrote in
    > message news:besdpq$8ge07$-berlin.de...
    > >
    > > "muser" <> 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
     
    John Harrison, Jul 14, 2003
    #3
    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. Samuele Armondi
    Replies:
    0
    Views:
    369
    Samuele Armondi
    Jul 13, 2003
  2. Venkat
    Replies:
    4
    Views:
    975
    Venkat
    Dec 5, 2003
  3. TrustyTif
    Replies:
    6
    Views:
    2,537
    B. Gandhi
    Jun 9, 2004
  4. Replies:
    2
    Views:
    11,233
    Dietmar Kuehl
    Jun 29, 2004
  5. Wirianto Djunaidi
    Replies:
    2
    Views:
    203
    Wirianto Djunaidi
    Apr 29, 2008
Loading...

Share This Page