Pointers and reference passing

This is a discussion on Pointers and reference passing within the C++ Programming forums, part of the General Programming Boards category; Hello what is the difference between functions accepting parameters as pointers and as reference? is it this difference " if ...

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    4

    Question Pointers and reference passing

    Hello

    what is the difference between functions accepting
    parameters as pointers and as reference?

    is it this difference
    " if you pass it as pointers you will be able to change the address of the paremeters but in case of reference you won't be able to change them or (can you change them really ?please update me on this) "

    if so then why do you make copy constructors parameters const references instead of simple reference passing ?

    Please help me on this

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    110
    Okay, I am not sure if this is entirely correct, but this is how I know it.

    Using pointers is a legacy from the days of function based C.

    the purpose of using constant referances in constructors and all ofther functions that do not need to change the variables being sent to the constructor / variable is for safety reasons and nothing else.

    Using a constant variable such as
    const int cantBeChanged = 5;
    will mean that the int cantBeChanged is stuck at 5 for the life of the program and as the naming suggests it CAN'T BE CHANGED.

    By using references you are referncing the memory that the variable is being stored in and any changes made in the function / constructor to the variable being passed are applied to the variable being passed.

    When you reference a variable by use of a pointer, you must remember that you are working with the memory that the variable is stored in, and things are different and often the source of annoying problems in large programs.

    Always try to pass variables as refernces, and if they don't need to be changed, as constant refernces.

    Hope you can find your way through my jibberish.

    Later,
    WebmasterMattD
    WebmasterMattD.NET

  3. #3
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    The diffrence is simply that refrences cann't be "re-refrenced"( to make it refrence to something else), but pointers can.
    And they both enable you to make direct changes to the arguments passed to the function.
    none...

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    pointers and references are similar, but different.

    pointers contain the address of another variable.
    references contain the address of another variable.
    the address in a pointer may be changed to a new address, unless it is a const pointer.
    the address in a reference must be initialized at declaration and it may not be changed.

    The value at the address in a pointer or a reference may be changed, unless the value is declared const.

    pointer need to be dereferenced explicitly to use the value at the address in the pointer.
    references are dereferenced behind the scenes--you don't have to do it.

    you can think of a reference as short hand for a const pointer.


    int num1 = 5;
    int num2 = 4;
    int * ptr;//dangling pointer, legal, but be careful
    int & refNum = num1;//must intialize at declaration

    ptr = &num1;//routine assignment of address to ptr

    ptr = &num2;//okay to change address in ptr

    refNum = &num2;//wrong, can't change address in reference

    *ptr = num2;//need to derefence pointer to change value

    refNum = num2;//don't need to dereference pointer to change value

    const int * ptr2;// the int pointed to is constant

    int * const ptr3; // ptr3 is constant, it can't point to anything else; this is essentially what a reference is

    const int * const ptr4;//neither value pointed to nor address in ptr4 can be changed

    const int & refNum2 = num1;//now the value of refNum2 can't be changed and refNum2 can only point to num1.

    a constant reference is essentially a constant pointer to a constant value as demonstrated above.

    void foo(int);//pass by value. changes to int in function won't chane value of parameter back in calling function

    void foo(int *, int &);//two pararameters passed by reference; the frist as a pointer, the second as a reference. the value of both parameters passed in can be changed in the function and it will change the value in the calling function as well. The address in the pointer may be changed by the function and it will change in the calling function, too; but the address in the reference may not be changed.

    void foo(const int &);//neither the address in the reference nor the value pointed to by the reference may be changed by the function

    void foo(const int * const )//the same as passing a const reference

  5. #5
    Registered User
    Join Date
    Aug 2002
    Posts
    4

    Smile Thanx for Refrences and Pointers update

    Thanx elad for updating me on the pointers and refrences
    It was very basic thing which I never knew over the rs
    Thanx again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing Pointers by reference
    By Bladactania in forum C Programming
    Replies: 10
    Last Post: 02-13-2009, 10:14 AM
  2. Passing pointers by reference
    By JOCAAN in forum C Programming
    Replies: 5
    Last Post: 12-03-2008, 11:02 PM
  3. Managed C++, passing arguments by reference
    By jimzy in forum C++ Programming
    Replies: 8
    Last Post: 11-02-2007, 02:03 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Passing Pointers By Reference
    By hern in forum C Programming
    Replies: 15
    Last Post: 07-29-2003, 12:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21