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.
*assuming fields within the class type.
Person person; //person is an object of class Person

*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.
-This now represents a pointer to a Person object:
person->Child.name // (If child can access its name by reference, it stays that way.)

Once again, this can be done with methods.

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);

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.

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

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.

  • emily

    Thanks for the detail.

    • Syazlee

      cant really understand..cant really understand

  • Anonymous

    nice explanation..thank you

  • Anonymous

    very nice to all of you , see the matter.

  • Miley Cyrus Family Pictures

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

  • nurilign

    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

    • Anon

      What’s the question?

  • http://twitter.com/syahmi_leh Muhamad Syahmi


  • Furqan9296

    thanks to provides me knowledge about pointers

  • blur kid


  • Wago

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

  • Abhishek

    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.

  • Amber

    Great job, William !
    Thanks very much!!