correct usage of pointers?

Discussion in 'C++' started by Eli Luong, Jun 8, 2006.

  1. Eli Luong

    Eli Luong Guest

    Hi,

    Sorry if I accidentally posted a blank post before.

    But, I was wondering, I'm practicing with pointers right now, and
    wanted to know if I am using it correctly in the following code. By
    correct I mean it follows convention, it should be the way I'm using
    pointers to do the swapping thing I'm trying below.

    Thanks.


    #include <iostream>
    #include <string>
    #include <fstream>
    #include <vector>

    using namespace std;

    // prototype functions
    void swap(double *x, double *y);

    int main()
    {
    double a = 10;
    double b = 20;
    double *ptr1 = &a;
    double *ptr2 = &b;

    cout << a << " " << b << endl;
    swap(ptr1, ptr2);
    cout << a << " " << b << endl;

    //system("pause");
    return 0;
    }

    void swap(double *x, double *y)
    {
    double temp = *x;
    *x = *y;
    *y = temp;
    }
    Eli Luong, Jun 8, 2006
    #1
    1. Advertising

  2. Eli Luong

    Guest

    Eli Luong пиÑал(а):

    > Hi,
    >
    > Sorry if I accidentally posted a blank post before.
    >
    > But, I was wondering, I'm practicing with pointers right now, and
    > wanted to know if I am using it correctly in the following code. By
    > correct I mean it follows convention, it should be the way I'm using
    > pointers to do the swapping thing I'm trying below.
    >
    > Thanks.
    >
    >
    > #include <iostream>
    > #include <string>
    > #include <fstream>
    > #include <vector>
    >
    > using namespace std;
    >
    > // prototype functions
    > void swap(double *x, double *y);
    >
    > int main()
    > {
    > double a = 10;
    > double b = 20;
    > double *ptr1 = &a;
    > double *ptr2 = &b;
    >
    > cout << a << " " << b << endl;
    > swap(ptr1, ptr2);
    > cout << a << " " << b << endl;
    >
    > //system("pause");
    > return 0;
    > }
    >
    > void swap(double *x, double *y)
    > {
    > double temp = *x;
    > *x = *y;
    > *y = temp;
    > }


    Hi.
    Sorry for my bad english.
    Your swap function do nothing for the caller, because you pass
    arguments by value.
    Look at the function prototype

    > void swap(double *x, double *y);


    Passing by reference solve the problem

    // prototype functions
    void swap(double *& x, double *& y);
    //...
    void swap(double *& x, double *& y)
    {
    double temp = *x;
    *x = *y;
    *y = temp;
    }
    , Jun 8, 2006
    #2
    1. Advertising

  3. Eli Luong

    Guest

    пиÑал(а):

    > Eli Luong пиÑал(а):
    >
    > > Hi,
    > >
    > > Sorry if I accidentally posted a blank post before.
    > >
    > > But, I was wondering, I'm practicing with pointers right now, and
    > > wanted to know if I am using it correctly in the following code. By
    > > correct I mean it follows convention, it should be the way I'm using
    > > pointers to do the swapping thing I'm trying below.
    > >
    > > Thanks.
    > >
    > >
    > > #include <iostream>
    > > #include <string>
    > > #include <fstream>
    > > #include <vector>
    > >
    > > using namespace std;
    > >
    > > // prototype functions
    > > void swap(double *x, double *y);
    > >
    > > int main()
    > > {
    > > double a = 10;
    > > double b = 20;
    > > double *ptr1 = &a;
    > > double *ptr2 = &b;
    > >
    > > cout << a << " " << b << endl;
    > > swap(ptr1, ptr2);
    > > cout << a << " " << b << endl;
    > >
    > > //system("pause");
    > > return 0;
    > > }
    > >
    > > void swap(double *x, double *y)
    > > {
    > > double temp = *x;
    > > *x = *y;
    > > *y = temp;
    > > }

    >
    > Hi.
    > Sorry for my bad english.
    > Your swap function do nothing for the caller, because you pass
    > arguments by value.
    > Look at the function prototype
    >
    > > void swap(double *x, double *y);

    >
    > Passing by reference solve the problem
    >
    > // prototype functions
    > void swap(double *& x, double *& y);
    > //...
    > void swap(double *& x, double *& y)
    > {
    > double temp = *x;
    > *x = *y;
    > *y = temp;
    > }


    Ooops... :eek:)
    My comprehension of question is wrong... Sorry.
    If you want to swap the values at wich x and y point your code is
    right...
    // prototype functions
    void swap(double * x, double * y);
    //...
    void swap(double * x, double * y)
    { // swap values
    double temp = *x;
    *x = *y;
    *y = temp;
    }

    To swap pointers the code look like this
    // prototype functions
    void swap(double *& x, double *& y);
    //...
    void swap(double *& x, double *& y)
    { // swap pointers
    double* temp = x;
    x = y;
    y = temp;
    }

    Best regards.
    , Jun 8, 2006
    #3
  4. Eli Luong

    Eli Luong Guest

    >
    > To swap pointers the code look like this
    > // prototype functions
    > void swap(double *& x, double *& y);
    > //...
    > void swap(double *& x, double *& y)
    > { // swap pointers
    > double* temp = x;
    > x = y;
    > y = temp;
    > }
    >
    > Best regards.


    Swapping pointers means you're going to change the memory address to
    which the pointer points to, right? So since you're trying to modify
    something inside a function, I know you use the & symbol to reference
    it, and then you just add the * symbol before it to mean it's a pointer
    type?
    Eli Luong, Jun 8, 2006
    #4
  5. Eli Luong

    Guest

    Eli Luong пиÑал(а):

    > >
    > > To swap pointers the code look like this
    > > // prototype functions
    > > void swap(double *& x, double *& y);
    > > //...
    > > void swap(double *& x, double *& y)
    > > { // swap pointers
    > > double* temp = x;
    > > x = y;
    > > y = temp;
    > > }
    > >
    > > Best regards.

    >
    > Swapping pointers means you're going to change the memory address to
    > which the pointer points to, right? So since you're trying to modify
    > something inside a function, I know you use the & symbol to reference
    > it, and then you just add the * symbol before it to mean it's a pointer
    > type?


    > Swapping pointers means you're going to change the memory address to
    > which the pointer points to, right?

    Yes.

    > So since you're trying to modify
    > something inside a function, I know you use the & symbol to reference
    > it, and then you just add the * symbol before it to mean it's a pointer
    > type?

    " *& " that means reference to pointer. So you pass pointer by
    reference not by value. In this case you alowed to change address to
    which the pointer points to.

    Fothemore you may use (and it's preferably) STL generic version of that
    function.
    It's better solution then write your own code.

    #include <algorithm> // std::swap
    #include <iostream> // std::cout

    int main(int, char *[])
    {
    double a = 1.0;
    double b = 2.0;
    double* a_ptr = &a, b_ptr = &b;
    std::cout<<"befor swapping: "<<"a = "<<*a_ptr<<", b =
    "<<*b_ptr<<std::endl;
    // swap pointers
    std::swap(a_ptr, b_ptr);
    std::cout<<"after swapping: "<<"a = "<<*a_ptr<<", b =
    "<<*b_ptr<<std::endl;

    double c = 3.0;
    double d = 4.0;
    std::cout<<"befor swapping: "<<"c = "<<c<<", d = "<<d<<std::endl;
    // swap variable values
    std::swap(a_ptr, b_ptr);
    std::cout<<"after swapping: "<<"c = "<<c<<", d = "<<d<<std::endl;
    return 0;
    }

    Best regards,
    Andrey
    , Jun 8, 2006
    #5
  6. Eli Luong

    Eli Luong Guest


    > >
    > > Swapping pointers means you're going to change the memory address to
    > > which the pointer points to, right? So since you're trying to modify
    > > something inside a function, I know you use the & symbol to reference
    > > it, and then you just add the * symbol before it to mean it's a pointer
    > > type?

    >
    > > Swapping pointers means you're going to change the memory address to
    > > which the pointer points to, right?

    > Yes.
    >
    > > So since you're trying to modify
    > > something inside a function, I know you use the & symbol to reference
    > > it, and then you just add the * symbol before it to mean it's a pointer
    > > type?

    > " *& " that means reference to pointer. So you pass pointer by
    > reference not by value. In this case you alowed to change address to
    > which the pointer points to.
    >
    > Fothemore you may use (and it's preferably) STL generic version of that
    > function.
    > It's better solution then write your own code.


    Thanks. I was just practicing to understand how pointers are working. I
    was writing up another little bit of code. In my other piece of code,
    the swapping worked correctly, but I was working with the dereferenced
    values. In this one, it's working directly with pointers, swapping the
    addresses of the pointers themselves, but once the function ends, it
    doesn't seem to have swapped at all, even though it's showing it being
    swapped in the function. I'm not sure how to look at this, because the
    general function parameters are exactly the same, it's just how I'm
    assigning the values inside the swap function that are different. Am I
    working with copies. I'm not sure if pointers are passed as new copies
    or as references.

    The line (int *temp = p1;) should be assigning to temp the same address
    that p1 is pointing to, along with the other code, so in the end p1 and
    p2 should have their dereferenced values exchanged, but that is not the
    case right now and I don't see why. Can you give some hints or
    something?

    // prototype functions
    void swap(int *p1, int *p2);

    int main()
    {
    int val1 = 10;
    int val2 = 30;
    int *ptr1 = &val1;
    int *ptr2 = &val2;
    swap(ptr1, ptr2);
    cout << val1 << val2 << endl;
    cout << *ptr1 << *ptr2 << endl;

    //system("pause");
    return 0;
    }

    void swap(int *p1, int *p2)
    {
    int *temp = p1;
    cout << *temp << " " << *p1 << endl;
    p1 = p2;
    cout << *p1 << *p2 << endl;
    p2 = temp;
    cout << *p1 << *p2 << endl;
    return;
    }
    Eli Luong, Jun 8, 2006
    #6
  7. Eli Luong

    Howard Guest

    "Eli Luong" <> wrote in message
    news:...
    > Hi,
    >
    > Sorry if I accidentally posted a blank post before.
    >
    > But, I was wondering, I'm practicing with pointers right now, and
    > wanted to know if I am using it correctly in the following code. By
    > correct I mean it follows convention, it should be the way I'm using
    > pointers to do the swapping thing I'm trying below.
    >
    > Thanks.
    >
    >
    > #include <iostream>
    > #include <string>
    > #include <fstream>
    > #include <vector>
    >
    > using namespace std;
    >
    > // prototype functions
    > void swap(double *x, double *y);
    >
    > int main()
    > {
    > double a = 10;
    > double b = 20;
    > double *ptr1 = &a;
    > double *ptr2 = &b;
    >
    > cout << a << " " << b << endl;
    > swap(ptr1, ptr2);
    > cout << a << " " << b << endl;
    >
    > //system("pause");
    > return 0;
    > }
    >
    > void swap(double *x, double *y)
    > {
    > double temp = *x;
    > *x = *y;
    > *y = temp;
    > }



    That code is fine.

    You don't actually need to create the pointers ptr1 and ptr2, though. You
    could just call "swap(&a,&b);", if you want.

    -Howard
    Howard, Jun 8, 2006
    #7
  8. Eli Luong

    Mak Guest

    Eli Luong пиÑал(а):

    > > >
    > > > Swapping pointers means you're going to change the memory address to
    > > > which the pointer points to, right? So since you're trying to modify
    > > > something inside a function, I know you use the & symbol to reference
    > > > it, and then you just add the * symbol before it to mean it's a pointer
    > > > type?

    > >
    > > > Swapping pointers means you're going to change the memory address to
    > > > which the pointer points to, right?

    > > Yes.
    > >
    > > > So since you're trying to modify
    > > > something inside a function, I know you use the & symbol to reference
    > > > it, and then you just add the * symbol before it to mean it's a pointer
    > > > type?

    > > " *& " that means reference to pointer. So you pass pointer by
    > > reference not by value. In this case you alowed to change address to
    > > which the pointer points to.
    > >
    > > Fothemore you may use (and it's preferably) STL generic version of that
    > > function.
    > > It's better solution then write your own code.

    >
    > Thanks. I was just practicing to understand how pointers are working. I
    > was writing up another little bit of code. In my other piece of code,
    > the swapping worked correctly, but I was working with the dereferenced
    > values. In this one, it's working directly with pointers, swapping the
    > addresses of the pointers themselves, but once the function ends, it
    > doesn't seem to have swapped at all, even though it's showing it being
    > swapped in the function. I'm not sure how to look at this, because the
    > general function parameters are exactly the same, it's just how I'm
    > assigning the values inside the swap function that are different. Am I
    > working with copies. I'm not sure if pointers are passed as new copies
    > or as references.
    >
    > The line (int *temp = p1;) should be assigning to temp the same address
    > that p1 is pointing to, along with the other code, so in the end p1 and
    > p2 should have their dereferenced values exchanged, but that is not the
    > case right now and I don't see why. Can you give some hints or
    > something?
    >
    > // prototype functions
    > void swap(int *p1, int *p2);
    >
    > int main()
    > {
    > int val1 = 10;
    > int val2 = 30;
    > int *ptr1 = &val1;
    > int *ptr2 = &val2;
    > swap(ptr1, ptr2);
    > cout << val1 << val2 << endl;
    > cout << *ptr1 << *ptr2 << endl;
    >
    > //system("pause");
    > return 0;
    > }
    >
    > void swap(int *p1, int *p2)
    > {
    > int *temp = p1;
    > cout << *temp << " " << *p1 << endl;
    > p1 = p2;
    > cout << *p1 << *p2 << endl;
    > p2 = temp;
    > cout << *p1 << *p2 << endl;
    > return;
    > }


    Your "swap" function receives it's arguments (p1, p2) by value. So the
    function operates with local copy of p1 and p2 of type pointer to int
    (int*). All changes occures inside function "swap" not inside outer
    scope.
    So if you want to swap two pointers this is wrong solution.
    You should change your function like this

    // prototype functions
    void swap(int *& p1, int *& p2);
    //...
    void swap(int *& p1, int *& p2)
    {
    int *temp = p1;
    cout << *temp << " " << *p1 << endl;
    p1 = p2;
    cout << *p1 << *p2 << endl;
    p2 = temp;
    cout << *p1 << *p2 << endl;
    return;
    }

    In this example "swap" function receive it's arguments p1 and p2. Where
    p1 and p2 of type nt*& (reference to pointer to int). So the function
    operates with local copy of reference to pointer to int (int*&) not
    with local copy of pointers to int (int*).
    Keep in mind value of pointer is address of memory to wich it points.

    Anothe way to swap the values of two pointers.
    // prototype functions
    void swap(int ** p1, int ** p2);
    //...
    void swap(int ** p1, int ** p2)
    {
    int *temp = *p1;
    *p1 = *p2;
    *p2 = temp;
    }
    In this case you should call function like this
    //...
    int a = 0, b = 1;
    int* ptr1 = &a;
    int* ptr2 = &b;
    swap(&ptr1, &ptr2);
    //...
    In this example swap function recieves it's aguments p1 and p2 of type
    pointer to pointer to int (int**). So we can change the values to wich
    they points to.

    Best regards,
    Andrey
    Mak, Jun 9, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. joon
    Replies:
    1
    Views:
    511
    Roedy Green
    Jul 8, 2003
  2. Dan

    correct or not correct?

    Dan, Oct 2, 2003, in forum: HTML
    Replies:
    7
    Views:
    435
  3. J.Ram
    Replies:
    7
    Views:
    642
  4. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    657
  5. froil
    Replies:
    12
    Views:
    305
    Gunnar Hjalmarsson
    Mar 2, 2006
Loading...

Share This Page