C++: Pointers, Pass by Value, Pass by Reference

You just cannot properly explain pointers without first knowing the basics of passing by value/reference.


I am sure someone who reads this will say, “But you can also pass a pointer.” While this is all well and true, the pointer passed is still a primitive variable and will, in turn, either be passed by reference or by value.

When passing a value, in either case, the call will look the same. It is the receiving method which determines how the pass is made.

By Value:

void aMethod(aParameter p) { }

When passing by value, a temporary COPY is made in memory taking up space and making any changes to the variable useless outside of the aMethod scope. Once control returns to the calling procedure, any changes to the variable will not be recognized, and you will still have the original variable.

By Reference:

void aMethod(aParameter& p) { }

NOTE: The & can be placed against the parameter type, against the parameter name, or there can be a space on either side.

This essentially passes a pointer but not the same kind of pointer you would get when creating the following:
Variable* v;
That is why it is called by reference. Since that is what you have, p now acts as if it were the parameter just like in the by value way, but any changes affect the original and stay changed outside of the aMethod scope.

*Now, if that parameter were a structure, class, or template, etc., it will have fields that are accessible by reference . or pointers ->.

In both cases of the above passing, the parameter passed can be accessed using the . or dot operator.
Examples:
*assuming fields within the class type.
Person person; //person is an object of class Person
person.name
person.Chlid.name

*It can also be used to call methods within the type.
person.getFriends() // (which would likely return a vector or array)

If the methods above were constructors or other member functions and accessed in a fashion like the method call just above, then there is a this operator for accessing the object which called it. Who’s fields can also be accessed but only by pointers.

Since the object is passed implicitly, it is passed as a pointer reference by default.
Examples:
-This now represents a pointer to a Person object:
this->name
person->Child.name // (If child can access its name by reference, it stays that way.)

Once again, this can be done with methods.
this->getFriends()

Other examples requiring pointers:
int* i = new int(0); //(This defines a dynamic int in memory pointed to by i.)
cout<<*i; (would print the value to which i points)

The * operator in both cases are NOT the same.
-The first is a pointer reference belonging to the type. In this case, it is int.
-The second is a de-referencing operator belonging to the variable which resolves it into the variable to which it is pointing.

Keep in mind that pointers are limitless:
int**** i = new int(1);
cout<<****i;

All that is needed is to de-reference as many times as is originally referenced, but you are probably thinking, "Why would anyone EVER do that?"
The quickest answer is for arrays.

Arrays are complicated and cumbersome to code. If there is a faster and easier way, we should do it.

By defining arrays by pointer, they are dynamic and can be created at run time, not based on static values. They can also be traversed in the same way, thanks to the C++ ingenuity of the following:
sizeOf and overloading the [] operator.
I will not discuss why or how this works. Maybe when I write the [] overloading function, I will explain it. Just know that it does.

Examples:
int* i = new int[3]; // (defines a pointer to an array of three ints)
cout<

It may seem strange, but it is a little complex for this recipe and involves the stack, bytes, and other pointers.

This again is limitless:
int** i = new int*[3]; // (define a pointer to an array of pointers to ints)
cout<<*i[0]; // (Now, we must de-reference the final referencing that is not array specific.)

Play around with pointers. They are the best part of C and C++, even though they can cause the most headaches when a * is misplaced. It opens up a whole new range of storage and arranging possibilities.

Questions/Comments: william_a_wilson@hotmail.com
-William. ยง (marvin_gohan)

The Conversation

Follow the reactions below and share your own thoughts.

13 Responses to “C++: Pointers, Pass by Value, Pass by Reference”

  1. September 01, 2009 at 8:37 pm, emily said:

    Thanks for the detail.

    Reply

    • January 06, 2011 at 4:08 am, Syazlee said:

      cant really understand..cant really understand

      Reply

  2. October 13, 2009 at 10:03 pm, Anonymous said:

    nice explanation..thank you

    Reply

  3. March 19, 2010 at 8:57 am, Anonymous said:

    very nice to all of you , see the matter.

    Reply

  4. May 06, 2010 at 11:12 am, Miley Cyrus Family Pictures said:

    Very clear and well explained! Thanks for the tutorial :)

    Reply

  5. June 25, 2010 at 10:12 am, nurilign said:

    i have one question
    1write a program which displays students name, and 5 subject mark, average and rank
    using structure mark is given by the user

    Reply

    • March 08, 2011 at 4:18 pm, Anon said:

      What’s the question?

      Reply

  6. November 01, 2010 at 6:05 am, Muhamad Syahmi said:

    ty

    Reply

  7. March 22, 2011 at 6:38 am, Furqan9296 said:

    thanks to provides me knowledge about pointers

    Reply

  8. June 27, 2011 at 1:53 am, blur kid said:

    blur

    Reply

  9. January 29, 2012 at 3:43 am, Wago said:

    An array can’t be returned in a function. A vector can.

    Reply

  10. October 17, 2012 at 3:57 pm, Abhishek said:

    Just a suggestion… if you could make the “code field” a bit more distinguishable, the tutorial will be much more easy to read and understand.

    Reply

  11. November 18, 2012 at 1:03 pm, Amber said:

    Great job, William !
    Thanks very much!!

    Reply

Leave a Reply