2

Can someone tell me if my understanding is right ? can someone tell me if the code below is for reference to pointers ?

# include <iostream>
using namespace std;

//function swaps references, 
//takes reference to int as input args and swap them
void swap(int& a, int& b)
{
    int c=a;
    a=b;
    b=c;
}

int main(void)
{
    int i=5,j=7;

    cout<<"Before swap"<<endl;
    cout<<"I:"<<i<<"J:"<<j<<endl;
    swap(i,j);
    cout<<"After swap"<<endl;
    cout<<"I:"<<i<<"J:"<<j<<endl;
    return 0;

}
ANjaNA
  • 1,404
  • 2
  • 16
  • 29
Jon Abraham
  • 851
  • 3
  • 14
  • 27
  • 2
    There are no pointers in your code. – chris Jul 15 '15 at 00:01
  • Can you please tell me how to use reference to pointers method ? – Jon Abraham Jul 15 '15 at 00:02
  • 1
    Can you please be more precise as to what you are trying to achieve? Your code is perfectly functional. You might want to read up on (and understand) the difference between a _pointer_ and a _reference_ in C++. For example [here](http://stackoverflow.com/questions/57483/what-are-the-differences-between-a-pointer-variable-and-a-reference-variable-in). – Jens Jul 15 '15 at 00:04
  • @Jens i am trying to achieve reference to pointer swap method – Jon Abraham Jul 15 '15 at 00:07
  • Please explain. In your code above you are swapping two integer values. – Jens Jul 15 '15 at 00:07
  • @Jens in my code i do notice that i am not using pointers as mentioned by chris. In swap function i am swapping reference and not pointers and in the main function i am calling the swap function. So do i need to use pointers instead of references in order to achieve reference to pointers method ? – Jon Abraham Jul 15 '15 at 00:11
  • @jon: See my answer below. – Jens Jul 15 '15 at 00:20

3 Answers3

3

You can create a reference to a pointer like this.

int i, j;        
int* ptr_i = &i;  //ptr_i hold a reference to a pointer
int* ptr_j = &j;
swap(ptr_i, ptr_j);

Function should be,

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

Note that:

  • a is the reference for the pointer, ptr_i in the above example.

  • *a dereferences what ptr_i point to, so you get the variable the pointer, ptr_i is pointing to.

For more refer this.

ANjaNA
  • 1,404
  • 2
  • 16
  • 29
1

In order to modify passing variables to a function, you should use reference (C-style pointers could also be a choice). If your objective is to swap pointers (in your case, addresses of the int variables) you should use reference to pointers and also pass to your swap function pointers (addresses of your int variables)

    # include <iostream>
    using namespace std;

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

    int main(void)
    {
        int i=5,j=7;
        int * p_i = &i;
        int * p_j = &j;

        cout << "Before swap" << endl;
        cout << "I:" << *p_i << "J:" << *p_j << endl;
        swap(p_i,p_j);
        cout << "After swap" << endl;
        cout << "I:" << *p_i << "J:" << *p_j <<endl;
        return 0;

   }
0

I would like to supplement everybody's answers with the standard conforming solution. It is good to know hot things like these work, but I find it better to use std::swap. This also has extra specializations a for containers, and it is generic for any type.

I know that this doesn't answer your question, but it is good to at least know that the standard is there.

Russell Greene
  • 2,141
  • 17
  • 29