-1

I have the following code

# include <iostream>
using namespace std;

void show (int list[], int len) {
    for (int i = 0; i < len; i++) {
        cout << list[i] << " ";
    }
    cout << endl;
}

void swap (int* a, int* b) {
    cout << *a << ' ' <<  *b << endl;
    int c = *a;
    *a = *b;
    *b = c;
    cout << *a << ' ' <<  *b << endl;
}

void aFunction (int list[], int len) {
    cout << "From aFunction ";
    show(list, len);
    swap(list[0], list[1]);
    cout << "From aFunction ";
    show(list, len);
}

int main() {
    int list[] = {6, 4};
    int len = sizeof(list)/sizeof(int);
    aFunction(list, len);
    return 0;
}

When I compile and run this I do not get any errors and I receive the output as

From aFunction 6 4 
From aFunction 4 6

But when I change the following line in aFunction

swap(list[0], list[1]);

to

swap(&list[0], &list[1]);

it still compiles and give me the following output

From aFunction 6 4 
6 4
4 6
From aFunction 4 6 

What is going on? My initial thought was swap(list[0], list[1]) is right since arrays decay to pointers when passed to a function so we are essentially passing pointers to swap but then the cout in swap is not printed, which leads me to believe swap(&list[0], &list[1]) is right. If the latter is right, why the compiler does not raise any error for the first and why the cout in swap does not execute? Which one is right? What's going on????? I am compiling this program using g++ weird.cpp on a MacBook Pro. I would be really grateful for an explanation.

Papa Delta
  • 267
  • 3
  • 12
  • 4
    You're calling [`std::swap`](https://en.cppreference.com/w/cpp/algorithm/swap) because of `using namespace std;` not your `swap` function. Yet another reason not to pull all of the `std` namespace into the global namespace like that. – Miles Budnek Aug 10 '21 at 03:32
  • Ahhhh, I see. Something is still not clear. Why/How is `list[0]` an `int` in `aFunction` when `list[]` is passed as an array to pointer when passing it to `aFunction`? If arrays are passed to functions as pointer then shouldn't `list[0]` in `aFunction` be of pointer to int type? – Papa Delta Aug 10 '21 at 03:51
  • 1
    @PapaDelta arrays can't be passed around by value, only by pointer/reference. In this case, the `int list[]` parameter of `aFunction()` is interpreted as `int* list`, so `list[n]` is the same as `*(list + n)`, which is an `int` (actually an `int&`). In `main()`, passing the `list` array to `aFunction()` will decay into an `int*` pointer to the 1st element. – Remy Lebeau Aug 10 '21 at 04:04
  • @RemyLebeau so every time I use `list[n]` in the function, it (`list + n`) is automatically getting dereferenced to `*(list+n)`? – Papa Delta Aug 10 '21 at 06:11

1 Answers1

3

You're calling std::swap, not your own swap function.

std::swap takes two reference arguments of any matching type.

(This is why, IMHO, using namespace std; can be a bad idea. I prefer to qualify names with std:: explicitly.)

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • Confirmed by viewing disassembly: https://godbolt.org/z/8s7onz7Wc – prehistoricpenguin Aug 10 '21 at 03:38
  • Ahhhh, I see. Something is still not clear. Why/How is `list[0]` an `int` in `aFunction` when `list[]` is passed as an array to pointer when passing it to `aFunction`? If arrays are passed to functions as pointer then shouldn't `list[0]` in `aFunction` be of pointer to int type? – Papa Delta Aug 10 '21 at 03:51
  • @PapaDelta I suggest reading section 6 of the [comp.lang.c FAQ](http://www.c-faq.com/). C and C++ have similar rules. – Keith Thompson Aug 10 '21 at 04:30