Thread: Reversing a string using pointers

  1. #1
    Registered User
    Join Date
    Jan 2017
    Posts
    20

    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
    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
    Last edited by asilvester635; 02-08-2017 at 12:28 AM.

  2. #2
    Registered User
    Join Date
    Jan 2017
    Posts
    20
    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. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > Though it gave me what I think is an address location. Below is the output. Why is that?
    It's not an address, it's garbage data caused by your out-of-bound memory access in reverse.

    > array[i] = array[size-i];
    When i=0, you have
    array[0] = array[size];

    As you should know, the valid subscripts for an array are 0 to size-1 inclusive.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reversing a string using pointers
    By Pulock2009 in forum C Programming
    Replies: 4
    Last Post: 11-14-2013, 03:14 AM
  2. Reversing Words and Array of Pointers
    By Striker87 in forum C Programming
    Replies: 3
    Last Post: 03-17-2011, 11:15 PM
  3. reversing a string, again..
    By Disident in forum C Programming
    Replies: 5
    Last Post: 06-15-2009, 08:01 PM
  4. reversing a string
    By quasigreat in forum C Programming
    Replies: 11
    Last Post: 05-22-2008, 06:24 AM
  5. Reversing A String?
    By Jazz in forum C++ Programming
    Replies: 4
    Last Post: 10-19-2001, 05:36 AM

Tags for this Thread