Reversing an array using pointers

Discussion in 'Programmer's Corner' started by asilvester635, Feb 8, 2017.

  1. asilvester635

    Thread Starter Member

    Jan 26, 2017
    68
    3
    I have this program that makes and populates an array. Then it is sent to a function called reverse, which reverses the order in the array. The compiler keeps giving errors. I'm not quite sure why.




    CODE
    Code (Text):
    1.  
    2. void reverse(int* array, int size) {
    3.  
    4.  
    5.    for (int i = 0; i < size/2; i++) {
    6.        int temp = array[i];
    7.        array[i] = array[size-i];
    8.        array[size-i] = temp;
    9.    } // end of for loop
    10.  
    11.  
    12. } // end of reverse
    13.  
    14.  
    15.  
    16.  
    17. int main( int argc, char** argv ) {
    18.  
    19.  
    20.    int array[8];
    21.  
    22.  
    23.    // get and print size of the array
    24.    int size = sizeof(array) / sizeof(array[0]);
    25.    printf("Size is %d\n", size);
    26.  
    27.  
    28.    // populate array
    29.    for (int i = 0; i < size; i++) {
    30.        array[i] = i;
    31.    } // end of for loop
    32.  
    33.  
    34.    // display array before reversing
    35.    for (int i = 0; i < size; i++) {
    36.        printf("%d ", array[i]);
    37.    } // end of for loop
    38.  
    39.  
    40.    // new line
    41.    printf("\n");
    42.  
    43.  
    44.    // reverse the array
    45.    reverse(&array, size);
    46.  
    47.  
    48.    // display the array again after reversing
    49.    for (int i = 0;i < size; i++) {
    50.        printf("%d ", array[i]);
    51.  
    52.  
    53.    } // end of for loop
    54. } // end of main
    55.  



    It keeps giving me this error
    main.cc:17:14: error: indirection requires pointer operand ('int' invalid)
    int temp = *array;
    ^~~~~~~~~
    main.cc:18:3: error: indirection requires pointer operand ('int' invalid)
    *array = *array[size-i];
    ^~~~~~~~~
    main.cc:18:15: error: indirection requires pointer operand ('int' invalid)
    *array = *array[size-i];
    ^~~~~~~~~~~~~~
    main.cc:19:3: error: indirection requires pointer operand ('int' invalid)
    *array[size-i] = temp;
    ^~~~~~~~~~~~~~
    4 errors generated.
    make: *** [main.o] Error 1
     
  2. asilvester635

    Thread Starter Member

    Jan 26, 2017
    68
    3
    I removed the & in front of the array variable when I send it to reverse. Though it gave me what I think is an address location. Below is the output. Why is that? There is no print statement in my code that intentionally prints any address.

    Output:
    0 1 2 3 4 5 6 7
    1412676568 7 6 5 4 3 2 1
    [Finished in 0.5s]
     
  3. JWHassler

    Member

    Sep 25, 2013
    231
    38
    Seems that 'reverse()' has an indexing problem... everybody is shifted down one slot
    'printf' has obediently displayed the value at array[0], which is wrong.
    The compiler-error output does not refer to the source-code version posted(?)
     
  4. MrAl

    Distinguished Member

    Jun 17, 2014
    3,227
    647
    Hi,

    It looks like (real quick look) the loop setup for swapping is not set up right for an array.

    If size=8 then the first items swapped are [0] and [8-0] which is just [0] and [8] when it should be [0] and [7] (which is [0] and [size-0-1]).

    The entire swap set for size=8 would be:
    0,7
    1,6
    2,5
    3,4

    thus the loop should go from 0 to 3 (k=0 to k<size/2) and the swap set would be size-k-1 not size-k.
    You use 'i' instead of 'k' here that's all.

    Checking:
    0: size-0-1=7 (ok)
    1: size-1-1=6 (ok)
    2: size-2-1=5 (ok)
    3: size-3-1=4 (ok)

    Alternately you could decrement size first:
    size=size-1; //will now be equal to 7
    Now:
    for (k=0;k<=size/2;k++) //note change to "<=" now
    {
    temp=array[k];
    array[k]=array[size-k];
    array[size-k]=temp;
    }

    See if that helps.
     
    Last edited: Feb 13, 2017
  5. MrSoftware

    Active Member

    Oct 29, 2013
    673
    185
    You had 3 main problems. I fixed it for you, and changed some variable names to try to help explain.

    (1) You were confused between the size of memory required for the array, and the number of elements in the array. When you access elements in the array, you use the element index, not the byte offset.
    (2) Second when you were reversing, don't forget that arrays are 0 based, meaning the first element is element 0, not element 1. So you have to add a -1 when you dereference an element.
    (3) The name of an array is a pointer to the first element, in this case a pointer to int. So when you call your reverse function, which takes an int*, just use the name "array" to get the pointer (type int*) to the first element of the array.

    Here's the working code, I hope this helps:

    Code (Text):
    1.  
    2.  
    3. void reverse(int* array, int NumberArrayElements) {
    4.  
    5.  
    6.   for (int i = 0; i < NumberArrayElements / 2; i++) {
    7.     int temp = array[i];
    8.     array[i] = array[NumberArrayElements - i - 1];    // -1 because the first element is element 0
    9.     array[NumberArrayElements - i - 1] = temp;
    10.   } // end of for loop
    11.  
    12.  
    13. } // end of reverse
    14.  
    15.  
    16.  
    17.  
    18. int main(int argc, char** argv) {
    19.  
    20.   const int NumberArrayElements = 8;
    21.   int array[NumberArrayElements];
    22.  
    23.  
    24.   // get and print size of the array
    25.   int size = sizeof(int) * NumberArrayElements;
    26.   printf("Size is %d\n", size);
    27.  
    28.  
    29.   // populate array
    30.   for (int i = 0; i < NumberArrayElements; i++) {
    31.     array[i] = i;
    32.   } // end of for loop
    33.  
    34.  
    35.     // display array before reversing
    36.   for (int i = 0; i < NumberArrayElements; i++) {
    37.     printf("%d ", array[i]);
    38.   } // end of for loop
    39.  
    40.  
    41.     // new line
    42.   printf("\n");
    43.  
    44.  
    45.   // reverse the array
    46.   reverse(array, NumberArrayElements);
    47.  
    48.  
    49.   // display the array again after reversing
    50.   for (int i = 0; i < NumberArrayElements; i++) {
    51.     printf("%d ", array[i]);
    52.   } // end of for loop
    53.  
    54. } // end of main
    55.  
    56.  
    57.  
    58.  
    59.  
     
Loading...