Initializing one array to another?

This is a discussion on Initializing one array to another? within the C++ Programming forums, part of the General Programming Boards category; how could i over come this error "ISO C++ forbids assignment of arrays"? heres the code; Code: Vehicles[Number_of_Vehicles].Parts[i].Condition = Condition; ...

  1. #1
    dac
    dac is offline
    Registered User
    Join Date
    Jan 2006
    Location
    North Yorkshire, England
    Posts
    147

    Initializing one array to another?

    how could i over come this error "ISO C++ forbids assignment of arrays"?

    heres the code;

    Code:
    Vehicles[Number_of_Vehicles].Parts[i].Condition = Condition;
    both Condition's are character arrays of 10. i just need them to be equal to each other.




    thanks,

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    memmove
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    dac
    dac is offline
    Registered User
    Join Date
    Jan 2006
    Location
    North Yorkshire, England
    Posts
    147
    Quote Originally Posted by vart
    memmove
    come again?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > both Condition's are character arrays of 10
    So make them both std::string which will improve the overall safety of your code, and get you the ability to assign them with =

    Or use strcpy()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    What you're probably looking for is the memcpy() function. It takes three parameters: the destination, the source, and the number of bytes to copy. In your case, you'd use it as follows:

    Code:
    memcpy(Vehicles[Number_of_Vehicles].Parts[i].Condition, Condition, 10*sizeof(char));
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,436
    if condition is an array, you can't copy one array into another.

    use memcpy or, better yet, std::vector.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by Salem
    > both Condition's are character arrays of 10
    So make them both std::string which will improve the overall safety of your code, and get you the ability to assign them with =

    Or use strcpy()
    it is if they are null-terminated strings, of course

    if it is just char-array and not a c-string memove or memcpy will do the trick
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #8
    dac
    dac is offline
    Registered User
    Join Date
    Jan 2006
    Location
    North Yorkshire, England
    Posts
    147
    thanks guys, memcpy has done the trick.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Initializing a 2D Array in C
    By Cell in forum C Programming
    Replies: 20
    Last Post: 03-21-2009, 12:31 PM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 08:51 AM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 12:01 AM
  5. Initializing char * array
    By Tia in forum C Programming
    Replies: 6
    Last Post: 03-11-2003, 04:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21