0

Notice in the code that I am not using pointers, but I had concepts that if I would use this function, the value would return back to normal when the code block is finished.

But the code is compiling with the answer which I would get with pointers actually.

I need help as I am confused if I have foul concept related to pointers.

void swap(int i, int j) {
    int temp = i;
    i = j;
    j = temp;
}

int main() {
    int a = 110;
    int b = 786;
    cout << "Before swapping the value" << endl;
    cout << "'a' stores the value : " << a << endl;
    cout << "'b' stores the value : " << b << endl;
    swap(a,b);
    cout << "\nAfter swapping the value" << endl;
    cout << "'a' stores the value : " << a << endl;
    cout << "'b' stores the value : " << b << endl;
    swap(a, b);
    cout << "\nAnd back again swapping the value" << endl;
    cout << "'a' stores the value : " << a << endl;
    cout << "'b' stores the value : " << b << endl;

    return 0;
}

I am getting results without using pointers - is this IDE problem

  • 2
    What is the actual output of your program? What output did you expect? Please edit your question to include that. – Some programmer dude Nov 22 '16 at 09:56
  • Sorry are you claiming that `swap`ing works or not as this does not work: http://ideone.com/xzGMcl as you're passing by value meaning it copies your args and doesn't assign any new value to them – EdChum Nov 22 '16 at 09:56
  • Perhaps attempt swap with a macro: `#define swap(i,j) do{int temp = i; i = j; j = temp;}while(0)` – GPS Nov 22 '16 at 09:59
  • Use `std::swap` to swap values or `std::swap_ranges` to swap more elements using iterators. – Jonas Nov 22 '16 at 10:02
  • The question here is why the swap seems to work for him. As shown in the screenshot OP posted as comment in an answer. It shouldn't work since he's just swapping in the context of the swap function without passing pointers or references. – Philipp Nov 22 '16 at 10:07
  • The image you linked in an answer shows several other swap functions - you need to paste the code which gives the results, not some other code. – doctorlove Nov 22 '16 at 10:08
  • Can you post *code* that reproduces the issue rather than a cropped screen shot of a bit of the code? – doctorlove Nov 22 '16 at 10:09
  • 2
    Do you declare the `swap` functions before you use them? Are you doing `using namespace std;` at the top? Please create a [Minimal, ***Complete***, and Verifiable Example](http://stackoverflow.com/help/mcve) and show us. – Some programmer dude Nov 22 '16 at 10:09

3 Answers3

6

Your swap function will not swap the values in the scope of main, because i and j are function local variables. To get the behavior you expect, you should pass by reference.

void swap(int& i, int& j) {
    int temp = i;
    i = j;
    j = temp;
}

Your code will not actually swap the values.

Guess:
I think that you are using namespace std; and from one of your #include from the standard library you are colliding with std::swap. I think the std:: version of the function is being called in your case, that is the only reason your code appears to "work".

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
3

It seems the iostream header you included also includes the utility header; and you get a definition of std::swap pulled into your program.

Since you (don't show it, but probably) have using namesapce std; in your code, the overload set for swap contains both overloads. And by the rules of overload resolution, the correct1 overload is called.


1 For some definition of correct, in this case

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
  • gonna change the function name and check it – Baqar Hussain Nov 22 '16 at 10:10
  • I created a project in Visual Studio 2013 with including the `iostream` header and `using namespace std;` I cannot reproduce OP's problem. The swap doesn't work which is the predicted behavior. – Philipp Nov 22 '16 at 10:12
  • @Philipp, you cannot assume any standard header includes others unless the standard guarantees it (which it doesn't in this case). [GCC reproduces it](http://ideone.com/k19F60), for instance. – StoryTeller - Unslander Monica Nov 22 '16 at 10:14
  • 1
    Thanks bro, this was the part i was missing. I have changed the function name, and its returning me the original values as it should have. – Baqar Hussain Nov 22 '16 at 10:14
  • @BaqarHussain, you don't have to give up simple names for you classes and functions, that's what [namespace](http://en.cppreference.com/w/cpp/language/namespace)s are for :) – StoryTeller - Unslander Monica Nov 22 '16 at 10:16
  • @StoryTeller Neatly spotted! :) – Vada Poché Nov 22 '16 at 10:16
  • @StoryTeller I see. So even within Visual Studio this could vary? – Philipp Nov 22 '16 at 10:16
  • 1
    @StoryTeller i am new with c++, had to learn it bcoz of my degree, i am working around it, and this community seems awesome :) – Baqar Hussain Nov 22 '16 at 10:17
  • 1
    @Philipp, Yup. A vendor update can mess up your codebase if you aren't careful. That's why you should always (1) include everything you need explicitly and (2) partition your code into namespaces to avoid collisions. – StoryTeller - Unslander Monica Nov 22 '16 at 10:18
  • @StoryTeller Nice, learned something important today then! :) – Philipp Nov 22 '16 at 10:20
  • 1
    @BaqarHussain If you're learning C++ and want to do it properly, we have a list of [good C++ books](http://stackoverflow.com/q/388242/1782465) which you might want to look into. – Angew is no longer proud of SO Nov 22 '16 at 10:24
  • @Angew btw i am doing c++ practice by watching pluralsight c++ paths, and reading this book "C-Plus-Plus-How-to-Program-8th-Edition-Paul-Deitel-Harvey-Deitel" - kindly guide me, as i get overwhelmed quite easily – Baqar Hussain Nov 22 '16 at 10:28
0

If you want to use pointers to swap, you should pass by pointers:

void swap(int *a, int*b)
{
  int temp = *a;
  *a = *b;
  *b = temp;
}

Passing by reference is another option, as another user has pointed out.

PS: Your query has nothing to do with function-pointers, so I'll be removing the function-pointer tag from your query.

Vada Poché
  • 763
  • 5
  • 16