简体   繁体   中英

Reversing an array using pointers

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

void reverse(int* array, int size) {

    for (int i = 0; i < size/2; i++) {
        int temp = array[i];
        array[i] = array[size-i];
        array[size-i] = temp;
    } // end of for loop

} // end of reverse 


int main( int argc, char** argv ) {

    int array[8];

    // get and print size of the array
    int size = sizeof(array) / sizeof(array[0]);
    printf("Size is %d\n", size);

    // populate array
    for (int i = 0; i < size; i++) {
        array[i] = i;
    } // end of for loop

    // display array before reversing
    for (int i = 0; i < size; i++) {
        printf("%d ", array[i]);
    } // end of for loop

    // new line
    printf("\n");

    // reverse the array
    reverse(&array, size);

    // display the array again after reversing
    for (int i = 0;i < size; i++) {
        printf("%d ", array[i]);

    } // end of for loop
} // end of main

It keeps giving me this error

main.cc:17:14: error: indirection requires pointer operand ('int' invalid)
                int temp = *array[i];
                           ^~~~~~~~~
main.cc:18:3: error: indirection requires pointer operand ('int' invalid)
                *array[i] = *array[size-i];
                ^~~~~~~~~
main.cc:18:15: error: indirection requires pointer operand ('int' invalid)
                *array[i] = *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

I did solve this problem a little differently, maybe you will use this code:

#include <iostream>

void displayArray(int table[], int size);

void rev(int table[], int size);

void fillTheArray(int table[], int size);

int main(int argc, char** argv) {

    int myArray[8];
    int size = sizeof(myArray) / sizeof(myArray[0]);
    std::cout << "Array size is: " << size << std::endl;

    fillTheArray(myArray, size);
    displayArray(myArray, size);
    std::cout << std::endl;

    rev(myArray, size);
    displayArray(myArray, size);

    std::cin.get();
    return 0;
}

void fillTheArray(int table[], int size) {
    for (int i = 0; i < size; i++) {
        table[i] = i;
    }
}

void displayArray(int table[], int size) {
    for (int i = 0; i < size; i++) {
        std::cout << table[i] << " ";
    }
    std::cout << std::endl;
}

void rev(int table[], int size) {

    int *start = table;
    int *end = table + (size - 1);

    for (int i = 0; i < size; i++) {

        if (start < end) {
            int temp = *end;
            *end = *start;
            *start = temp;
        }

        start++;
        end--;
    }
}

I can see two errors in this code. First is: wrong way of passing parametr to function:

 // reverse the array
 reverse(&array, size);

you should do this just like this(array name is pointer to first element of this array):

reverse(array, size);

Second problem is with reverser - you try to access some random memory outside arrar range:

array[i] = array[size-i]; 

Remember that in C++ array index's start with 0 not 1. So if your array is size of 8 - largest insext of this array is 7 (0, 1, 2, 3, 4, 5, 6, 7). Your code should look like this:

array[i] = array[size -i -1];

And it should work as you expected.

It is my solution with pointers:

void reverse(int arr[], int count)
{
    int* head = arr;
    int* tail = arr + count - 1;
    for (int i = 0; i < count/2; ++i)
    {
        if (head < tail)
        {
            int tmp = *tail;
            *tail = *head;
            *head = tmp;

            head++; tail--;
        }
    }

    for (int i = 0; i < count; ++i)
    {
        std::cout << arr[i] << " ";
    }
}

or just use functions build in C++: std::reverse in 'algorithm' library.

It's a lot of examples on stackoverflow with this kind of examples: Reverse Contents in Array

You have fixed most of the compiler errors in your code except one.

The line

reverse(&array, size);

should be

reverse(array, size);

After that is fixed, you have to fix logic errors in reverse .

You are using the wrong index for accessing the upper half of the array.

void reverse(int* array, int size) {

    for (int i = 0; i < size/2; i++) {
        int temp = array[i];
        array[i] = array[size-i];  // When i is 0, you are accessing array[size]
                                   // That is incorrect.
        array[size-i] = temp;
    } // end of for loop

} // end

You need to use

void reverse(int* array, int size) {
    for (int i = 0; i < size/2; i++) {
        int temp = array[i];
        array[i] = array[size-i-1];
        array[size-i-1] = temp;
    }
}

Another way to approach the algorithm would be to use two indices.

void reverse(int* array, int size) {
    for (int i = 0, j = size-1; i < j; ++i, --j) {
        int temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }
}

Working program: http://ideone.com/ReVnGR .

You're passing **int instead of *int to the reverse method:

reverse(&array, size);

Pass it like that:

reverse(array, size);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM