CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2017
    Posts
    17

    [RESOLVED] 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

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Reversing an array using pointers

    Code:
    int temp = *array[i];
    Have you fixed the errors because this line from the error list is different from the line in the code? The line in the code is
    Code:
    int temp = array[i];
    which seems OK. Same for the other errors.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Jan 2017
    Posts
    17

    Re: Reversing an array using pointers

    Thanks. I fixed it.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured