Results 1 to 2 of 2

Thread: Confusion about pointers and references in C++

  1. #1

    Thread Starter
    New Member Harshjain's Avatar
    Join Date
    Jul 2022
    Posts
    3

    Confusion about pointers and references in C++

    I have a bunch of code like this:

    Code:
    #include <iostream>
    using namespace std;
    
    void swap(int *a, int *b) {
        int temp = *a;
        *a = *b;
        *b = temp;
    }
    
    int main() {
        int a;
        int b;
        a = 7;
        b = 5;
        swap(a, b);
        cout << a << b;
    
        return 0;
    }
    This code does the swapping process as what I precisely needed to swap 2 numbers, however when I need two numbers from the user as follows;

    Code:
    int a;
    int b;
    cin >> a;
    cin >> b;
    swap(a, b);
    cout << a <<
    the compiler gives me an error about int to int* error which is as expected. Why does the first code do the right swapping although I didn't use the method with & operator?

  2. #2
    Frenzied Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    1,169

    Re: Confusion about pointers and references in C++

    Interesting question. The real question is why the first code compiles. The given swap() function takes 2 args of pointers to ints. However you are passing ints in the function call. So why does this compile and work?

    The answer is in the using statement. C++ has a std::swap() function. With the using statement (and iostream also using utility) then the compiler matches the swap() call to the C++ standard one and not the provided one.

    Try this - and you should get the error:

    Code:
    #include <iostream>
    //using namespace std;
    
    void swap(int* a, int* b) {
    	int temp = *a;
    	*a = *b;
    	*b = temp;
    }
    
    int main() {
    	int a;
    	int b;
    	a = 7;
    	b = 5;
    	swap(a, b);
    	std::cout << a << b;
    
    	return 0;
    }
    The moral is - If you use using namespace std, don't have functions with the same name as those used by C++.

    This is OK:

    Code:
    #include <iostream>
    //using namespace std;
    
    void swap(int* a, int* b) {
    	int temp = *a;
    	*a = *b;
    	*b = temp;
    }
    
    int main() {
    	int a;
    	int b;
    	a = 7;
    	b = 5;
    	swap(&a, &b);
    	std::cout << a << b;
    
    	return 0;
    }
    or just:

    Code:
    #include <iostream>
    //using namespace std;
    
    int main() {
    	int a;
    	int b;
    	a = 7;
    	b = 5;
    	std::swap(a, b);
    	std::cout << a << b;
    
    	return 0;
    }
    Note that you should have #include <utility> instead if relying upon this being provided by other includes.

    See https://cplusplus.com/reference/utility/swap/
    All advice is offered in good faith only. 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/

    C++23 Compiler: Microsoft VS2022 (17.6.5)

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