Code About Us Tutorial


  Functions
    What are Functions?
    Function Definition
    Functions as Parameters to Functions
    Return Values
    Separating Functions
    Passing By Value
    Passing By Reference
    Passing By Pointer
    Inline Functions
    Overloading Functions


   
 

Home > Functions > Passing By Pointer

November 27, 2009 10:59 pm

   

Passing By Pointer

Now you know how to pass by refference and constant reference, well basically you might consider this to be a bit more advance but if you have successfully understood pointers, then I guess this section should be pretty understandable to you.

Lets review pointers first, as they tend to be very challenging to most people.

A Pointer is a 4-byte variable that holds the memory location of the actual variable that it is pointing to. It could point to pretty much anything in memory, including functions, classes, variables, and even other pointers.

width=1
width=1

Pointers are tricky because most people are not used to dealing with memory and C/C++ is pretty much based on memory handling. It is a mix of a low level and a high level programming language. Now enough about that, lets get back to discussing pointers. A pointer could point to any variable or another pointer. Infact, it could also point to another application or a dll in the memory stack, but we will not get into that.

If you read the section on pointers you know that pointers are declared like this:

For style preferences and to make debugging life a bit easier, it is recommended that you prefix your pointer variable name with the letter 'p'.

width=1
width=1

The syntax of a pointer goes like:

 

 
<type pointing to> * <variable name> = <initialization>;
// or without initialization
<type pointing to> * <variable name>;

The type pointing to could be any type and variable name is the name of the variable, initialization could be either the variable you want the pointer to point to, or 0 or NULL which is basically just a 0 internally.

 

 
// pointer review
// ...
float amount = 175.95;
int * pNum; // a pointer to an int
char * pStr; // a pointer to a char or string
float * pPrice = &amount; // pointer to a float initialized to amount.

Notice that the variable name of the variable the pointer is pointing to (amount) is prefixed with an ampersand ('&'), that is because to get the pointer of that variable, you need to prefix it with an ampersand.

Now lets forward a bit to how variables are sent to functions by pointer.

 

 
// example of pass by pointer
#include <iostream.h>

// a function with a ridicilus name void add_1_dollar_and_50_cents(float * pAmount ) { // in order to alter the actual variable // that the pointer is pointing to, // you have to dereference it by prefixing // it with a star (*) cout << pAmount; // will print the MEMORY ADRESS of the // variable that pAmount is pointing to. cout << *pAmount; // will print the VALUE of the variable // pAmount is pointing to.

*pAmount += 1.50F; // suffix it with F to specifically make it a float

}

int maint(void) { // garage sale demo float amount = 63.95; // original price float myamount = 25.00; // your price

float * pAmount = &amount; // pointer to amount

// There are several ways of passing by pointer cout << amount << endl; // 63.95

add_1_dollar_and_50_cents(pAmount); // passing the pointer variable

cout << amount << endl; // 65.45 cout << *pAmount << endl; // exact same as amount (65.45)

add_1_dollar_and_50_cents(myamount); // error - expecting pointer to a float (missing '&')

add_1_dollar_and_50_cents(&pAmount); // error - expecting pointer // to a float not a double pointer

return 0; }

So to make a long story short, passing by pointer is not as hard as many people precieve it to be. It just needs a little more practice since we're not used to this kind of thing. Also, remember that ANYTHING can be passed by pointer, and I mean anything in C++. So keep that in mind and see you in the next section!Please Rate this Code:


    Comments for: Passing By Pointer
Annonymouse User says:
could you make your pages a little less wide so that I don\'t have to move the bar back a forth. Other wise thanks for the tutorial. Bill




Add Comment:
Name:
Email:


 «1» THINKQUEST TEAM C0111571 © 2001. All Rights Reserved.