-1
#include <iostream>
using namespace std;

int* reverse(int arr[],int n){
    int rev[100];
    int j =0;
    for(int i=n-1;i>=0;i--){
            rev[j]=arr[i];
            j++;
            }
    return rev;
}
int main() {
    int n;
    cin>>n;
    int arr[100];
    for(int i=0;i<n;i++){
        cin>>arr[i];
    }
    cout<<reverse(arr,n);
}

I am trying reverse an array using loops but don't know what the error was it was returning some bin value.

  • 1
    Because you are returning an int pointer. – Samarthya Singh Sep 16 '22 at 13:57
  • You are printing out a pointer. You cannot print an array that way. Also, that pointer is to a local variable, so you have UB if it tried to actually print its contents. Start by looking up how to print an array forwards, then worry about reversing. – ChrisMM Sep 16 '22 at 13:57
  • It's returning a pointer, whose value is printed in hexadecimal notation. You don't need the other array – reverse the input in place instead. (Think "swap" instead of "copy".) – molbdnilo Sep 16 '22 at 13:57
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Jesper Juhl Sep 16 '22 at 13:58
  • 2
    If possible, please invest in [some good C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282), and learn about `std::vector` (and other standard functionality). – Some programmer dude Sep 16 '22 at 13:59
  • Also you are returning a pointer to a variable that no longer exists after the function ends. If you dereference the pointer you will have UB. – drescherjm Sep 16 '22 at 14:00
  • You need to tell us if this is an academix exercise or not. If not, always consider using C++ standard library algorithms. For your array reversal there is `std::reverse`. For your output, you can do it with `std::copy`. – Paul Floyd Sep 16 '22 at 15:43
  • Thank you so much for clarifying my doubt in an efficient way !! Thanking you all – hemanth_101 Sep 17 '22 at 01:48

1 Answers1

0

Your rev temporary resides in automatic storage. It means that the object will be gone after the function returns. While C++ allows you to decay rev to an int* and then return said pointer, it does not mean that this returns the object itself. You merely get a pointer to an already destroyed object. Not very useful. In fact, doing anything with this pointer will cause undefined behaviour.

Usually what you want to do is reverse things in-place. That's also how std::reverse works.

So, there are two options. If you have a completely filled c-style array, you could write a reverse function like this:

template <std::size_t N>
void reverse(int (&a)[N]) {
  // reverse a from 0 to N-1
}
reverse(a);

Or, if you have an only partially filled array, take a page out of the standard library and reverse a range, denoted by two iterators.

void reverse(int* begin, int* end) {
  /* begin points to the first entry, end points one past the last */
}
reverse(a, a+n);

Of course, instead of using c-style arrays, you could use a dynamically growing array such as std::vector, which carries the actual size of the array around for you.

bitmask
  • 32,434
  • 14
  • 99
  • 159