-4

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
4
  • 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] Commented Feb 8, 2017 at 6:41
  • Your reverse function has an off-by-one error. Think about what happens when i == 0. Commented Feb 8, 2017 at 6:47
  • 1
    Your code says int temp = array[i];, the error says int temp = *array[i];. The posted code is correct. Please make sure that the code you post produces the error you are asking about in future. Commented Feb 8, 2017 at 6:53
  • @ConradoSanchez: That is a different question. You need to ask separately. (But it is the off-by-one error that people have told you about twice). Commented Feb 8, 2017 at 6:58

4 Answers 4

0

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--;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

-1

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.

2 Comments

This is not the OP's problem either. This should be a comment.
@MartinBonner, I noticed that the OP had updated the code to remove all the other errors. Thanks for pointing it out.
-2

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

reverse(&array, size);

Pass it like that:

reverse(array, size);

3 Comments

This is not the OPs problem. The problem is that the posted code doesn't have the error he is complaining about.
@MartinBonner Are you downvoting all these answers ?
I have downvoted answers which don't answer the question the OP has asked. (Making additional comments in an answer which does answer the OPs question is fine, so is explaining why the OP has asked the wrong question, and answering the question they should have asked - but just pointing out other problems should only be a comment)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.