0

This is a post from https://www.geeksforgeeks.org about reversing an array. In the rvereseArray() function, array is not using any pointer. But still in the main() function, when the rvereseArray() function is called and the arr is passed, it is able to alter the value. how?

// Iterative C++ program to reverse an array
#include <bits/stdc++.h>
using namespace std;

/* Function to reverse arr[] from start to end*/
void rvereseArray(int arr[], int start, int end)
{
    while (start < end)
    {
        int temp = arr[start];
        arr[start] = arr[end];
        arr[end] = temp;
        start++;
        end--;
    }
}   

/* Utility function to print an array */
void printArray(int arr[], int size)
{
for (int i = 0; i < size; i++)
cout << arr[i] << " ";

cout << endl;
}

/* Driver function to test above functions */
int main()
{
    int arr[] = {1, 2, 3, 4, 5, 6};
    
    int n = sizeof(arr) / sizeof(arr[0]);

    // To print original array
    printArray(arr, n);
    
    // Function calling
    rvereseArray(arr, 0, n-1);
    
    cout << "Reversed array is" << endl;
    
    // To print the Reversed array
    printArray(arr, n);
    
    return 0;
}
user4581301
  • 33,082
  • 7
  • 33
  • 54
Noob101
  • 13
  • 2
  • 3
    You may want to read [Why should I _not_ #include ?](https://stackoverflow.com/q/31816095/11082165) and [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/q/1452721/11082165) – Brian61354270 Nov 16 '21 at 22:40
  • 1
    `int n = sizeof(arr) / sizeof(arr[0]);` -> `int n = std::size(arr);` if your compiler is relatively up to date. – user4581301 Nov 16 '21 at 22:41
  • _"array is not using any pointer"_ Short answer: `arr` **is** a pointer in all functions except `main`. – Drew Dormann Nov 16 '21 at 23:01

0 Answers0