Like Tree7Likes
  • 2 Post By laserlight
  • 1 Post By Click_here
  • 3 Post By laserlight
  • 1 Post By laserlight

Copying from one array to another...

This is a discussion on Copying from one array to another... within the C Programming forums, part of the General Programming Boards category; G'day! Just a little background, I'm a first year student who is just starting to learn C89 (Yes, I have ...

  1. #1
    Time-Lord Victorious! The Doctor's Avatar
    Join Date
    Aug 2012
    Location
    Perth, Western Australia
    Posts
    50

    Copying from one array to another...

    G'day! Just a little background, I'm a first year student who is just starting to learn C89 (Yes, I have to use C89). I learned a little Java earlier this year, and am now learning C89 and more Java at the same time.

    The following code is supposed to put an array into reverse order. I'm creating a temporary array, and then trying to copy all the values back into the original array, to go back to the function calling this fuction.

    I get a segmentation fault just after it prints "DEBUG". On line 19 I'd imagine.

    Could someone enlighten me as to what I'm doing wrong?


    Code:
     
     void reverse( int array[], int length)
    {
            int ii, temp;
            int jj = 0;
            int *tempArray = (int)malloc( length * sizeof( int));
    
            for ( ii = length-1; ii >= 0; ii--)
            {
                            tempArray[jj] = array[ii];
                            jj += 1;
            }
    
            array = *tempArray;
    
            for (ii = 0; ii <= length-1; ii++)
            {
                     printf("DEBUG\n");
    
                    temp = tempArray[ii];
                    array[ii] = temp;
    
            }
    
            free( tempArray);
    }
    thanks,
    Last edited by The Doctor; 08-28-2012 at 09:39 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,598
    Quote Originally Posted by The Doctor
    The following code is supposed to put an array into reverse order. I'm creating a temporary array, and then trying to copy all the values back into the original array, to go back to the function calling this fuction.
    Instead of creating a temporary array, think of how you could swap the first element with the last element, the second element with the second last element, etc, to reverse the array in-place.

    Anyway, this is wrong:
    Code:
    array = *tempArray;
    array is a pointer to int, *tempArray is an int, so assigning one to the other does not make sense here.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Time-Lord Victorious! The Doctor's Avatar
    Join Date
    Aug 2012
    Location
    Perth, Western Australia
    Posts
    50
    Thanks! Actually, on looking at it again, I don't actually need that line. The for loop does what that line was meant to. (and it works...)

    I actually tried to do it in place originally, but a fellow student kept pointing out flaws in my code, and I wasn't sure how to fix them at the time.



    By the way, when I compile, It comes up with these warnings:

    manage_array.c: In function ‘reverse’:
    manage_array.c:42:19: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
    manage_array.c:42:19: warning: initialization makes pointer from integer without a cast [enabled by default]
    (There's more functions in manage_array.c, as you can probably guess...

    Line 42 is my malloc statement.

    Code:
    void reverse( int array[], int length)
    {
            int ii, temp;
            int jj = 0;
            int *tempArray = (int)malloc( length * sizeof( int));
    
            for ( ii = length-1; ii >= 0; ii--)
            {
                            tempArray[jj] = array[ii];
                            jj += 1;
            }
    
            for (ii = 0; ii <= length-1; ii++)
            {
    
    
                    temp = tempArray[ii];
                    array[ii] = temp;
    
            }
    
            free( tempArray);
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,598
    Quote Originally Posted by The Doctor
    I actually tried to do it in place originally, but a fellow student kept pointing out flaws in my code, and I wasn't sure how to fix them at the time.
    Then see if you now are able to fix those flaws

    Quote Originally Posted by The Doctor
    By the way, when I compile, It comes up with these warnings:
    Yeah, you should not be casting the return value of malloc. You casted it incorrectly, i.e., if you did want to cast, you should cast to int* not int.

    Also, instead of writing ii <= length-1 just write ii < length. You don't need the temp variable (though it would come it handy for the in-place reverse).
    iMalc and The Doctor like this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,343
    int*tempArray = (int *)malloc( length * sizeof( int));
    The Doctor likes this.

  6. #6
    Time-Lord Victorious! The Doctor's Avatar
    Join Date
    Aug 2012
    Location
    Perth, Western Australia
    Posts
    50
    Derp, yes of course... Thanks guys!

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,598
    Quote Originally Posted by Click_here
    int*tempArray = (int *)malloc( length * sizeof( int));
    I recommend:
    Code:
    int *tempArray = malloc(length * sizeof(*tempArray));
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Time-Lord Victorious! The Doctor's Avatar
    Join Date
    Aug 2012
    Location
    Perth, Western Australia
    Posts
    50
    Quote Originally Posted by laserlight View Post
    I recommend:
    Code:
    int *tempArray = malloc(length * sizeof(*tempArray));

    But wait, why would we be using the sizeof(*tempArray)? I don't get it. Aren't we just defining *tempArray there? Do you mean the size of the other array?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,598
    Quote Originally Posted by The Doctor
    why would we be using the sizeof(*tempArray)?
    The idea is that all the elements of the array are of the same size, so we take the size of the first element, i.e., sizeof(*tempArray). Granted, the dynamic array does not exist yet, but that is not a problem because sizeof is evaluated at compile time here.
    rags_to_riches likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. copying array?
    By nikosmilo in forum C Programming
    Replies: 4
    Last Post: 07-30-2011, 06:54 AM
  2. Copying row array help!!
    By problem in forum C Programming
    Replies: 1
    Last Post: 04-22-2011, 05:51 PM
  3. Array Copying
    By Prodiga1 in forum C Programming
    Replies: 2
    Last Post: 03-01-2011, 03:18 PM
  4. need help in copying unsigned char array to bit array
    By lovesunset21 in forum C Programming
    Replies: 8
    Last Post: 10-29-2010, 07:23 AM
  5. array copying
    By kurz7 in forum C Programming
    Replies: 2
    Last Post: 05-07-2003, 01:43 AM

Tags for this Thread


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