Thread: Have problems with copying my array!

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You should not use strcpy() on anything that isn't STRINGS. If you want to use a library function to copy generic data, then you should use memcpy().

    Edit: And of course, with code like this:
    Code:
       int array[ SIZE ]; /* declare the array of ints to be sorted (test with bublesort)*/
       int copy[SIZE];/*copy of original array to test with selectionsort*/
       strcpy(array ,copy );
    even if we replace strcpy with something that copies the array properly, it will just copy garbage, since array and copy contain nothing useful until you have filled them in with something meaningful. So you are just copying random gunk, really [and you're just lucky that the program doesn't crash at that point - obviously there is a zero somewhere in the data before you've done too much damage by using strcpy to copy something that isn't a string].

    --
    Mats
    Last edited by matsp; 09-24-2008 at 11:40 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    54
    Quote Originally Posted by matsp View Post
    You should not use strcpy() on anything that isn't STRINGS. If you want to use a library function to copy generic data, then you should use memcpy().

    --
    Mats
    Yes sorry love, i just finished reading that part. So memcpy would solve the problem?

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by AvaGodess View Post
    Yes sorry love, i just finished reading that part. So memcpy would solve the problem?
    Please read my edit as well.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    54
    Quote Originally Posted by matsp View Post
    Please read my edit as well.

    --
    Mats
    I know at that point it has basically nothing. But arent I feeding it some data in this part
    Code:
    for ( i = 0; i < SIZE; i++ )
          array[ i ] = rand() % 90 + 10; /* give each element a value */
           
       printf( "Unsorted array:\n" );
        
       for ( i = 0; i < SIZE; i++ ) /* print the array */
          printf( "%d  ", array[ i ] );
    I'm just asking for some help thats all, how could this little program be settled. Without doing what you say. Basically i wanted to make copies of the elements given to the array, and work with them in the function.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by AvaGodess View Post
    I know at that point it has basically nothing. But arent I feeding it some data in this part
    Code:
    for ( i = 0; i < SIZE; i++ )
          array[ i ] = rand() % 90 + 10; /* give each element a value */
           
       printf( "Unsorted array:\n" );
        
       for ( i = 0; i < SIZE; i++ ) /* print the array */
          printf( "%d  ", array[ i ] );
    I'm just asking for some help thats all, how could this little program be settled. Without doing what you say. Basically i wanted to make copies of the elements given to the array, and work with them in the function.
    Yes, but that is AFTER you called strcpy(). My point was that if we assume that you replace strcpy() with something that actually copies array into copy, it would still contain garbage, since array has NOT YET been filled with something - it contains whatever happens to be in that location of memory [1], and it's very unlikely that this matches your random number sequence.

    [1] Some compilers will actually fill unused memory with a recognisable pattern so that we can "see" that the memory has not been filled in, but the content is still not what you would like to have, so it's pretty irrelevant really.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    54
    Quote Originally Posted by matsp View Post
    Yes, but that is AFTER you called strcpy(). My point was that if we assume that you replace strcpy() with something that actually copies array into copy, it would still contain garbage, since array has NOT YET been filled with something - it contains whatever happens to be in that location of memory [1], and it's very unlikely that this matches your random number sequence.

    [1] Some compilers will actually fill unused memory with a recognisable pattern so that we can "see" that the memory has not been filled in, but the content is still not what you would like to have, so it's pretty irrelevant really.

    --
    Mats
    Thank u very much Matt, i understand what u are saying now. As soon as get near a compiler ill follow all recommendations given. Thank u very much!!

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by AvaGodess View Post
    I know at that point it has basically nothing. But arent I feeding it some data in this part
    Code:
    for ( i = 0; i < SIZE; i++ )
          array[ i ] = rand() % 90 + 10; /* give each element a value */
           
       printf( "Unsorted array:\n" );
        
       for ( i = 0; i < SIZE; i++ ) /* print the array */
          printf( "%d  ", array[ i ] );
    I'm just asking for some help thats all, how could this little program be settled. Without doing what you say. Basically i wanted to make copies of the elements given to the array, and work with them in the function.
    The two arrays are not "magically" linked together.
    They are separate arrays, with separate allocated space.
    What you do with one, does not affect the other.
    Therefore, if you want to duplicate the array, you should do so AFTER you've input the data.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Sep 2008
    Posts
    54
    Quote Originally Posted by Elysia View Post
    The two arrays are not "magically" linked together.
    They are separate arrays, with separate allocated space.
    What you do with one, does not affect the other.
    Therefore, if you want to duplicate the array, you should do so AFTER you've input the data.
    Thank u very much Elysia, yes now im begning to understand the concepts of C. So im assuming if i make my copy after the array has been filled it will work right, like Matts had said before or simple declaring it as what he said arra[i]=copy[1]

    Oo im begning to love C, but its a bit tricky i think im gona enjoy this language!

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by AvaGodess View Post
    Oo im begning to love C, but its a bit tricky i think im gona enjoy this language!
    If you like C, then you're going to love C++.
    Just a friendly advice
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by AvaGodess View Post
    Yes sorry love, i just finished reading that part. So memcpy would solve the problem?
    Think simple.
    Code:
    copy[i] = array[ i ] = rand() &#37; 90 + 10;
    da da!

    Is your nickname inspired from Sin City (curious) ?

    EDIT: Yes, memcpy() would work. But, in any case, you can always just use a loop and copy each individual element of the array.
    Last edited by C_ntua; 09-24-2008 at 11:53 AM.

  11. #11
    Registered User
    Join Date
    Sep 2008
    Posts
    54
    Quote Originally Posted by C_ntua View Post
    Think simple.
    Code:
    copy[i] = array[ i ] = rand() % 90 + 10;
    da da!

    Is your nickname inspired from Sin City (curious) ?

    EDIT: Yes, memcpy() would work. But, in any case, you can always just use a loop and copy each individual element of the array.
    Thats gorgeous, now im starting to think what my friends say bout blondes lol. How come i didnt see that, thats beautiful and simple, as soon as i get home near a compiler ill try it. Lol, hmm Sin City i heard when the movie was coming out but never actually went to see it so im not particularly sure about ur definition. But lad, my name well im Ava, and believe my beauty makes me a Godess, lol just kidding. Thank u very much love, ill let u know when i solve it at home, will be in a couple of hours. right now here at a cafe u see

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with passing an array of structures by pointer
    By raptor1770 in forum C Programming
    Replies: 9
    Last Post: 11-29-2008, 11:01 AM
  2. Problem(s) with an array!!
    By Leojeen in forum C Programming
    Replies: 6
    Last Post: 05-02-2008, 07:26 PM
  3. copying values of an array to another array?!
    By webznz in forum C Programming
    Replies: 8
    Last Post: 10-24-2007, 10:59 AM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM