Pointers and Functions:

Rules about passing pointers into functions is no different from passing any other type of variable.

Passing a pointer by value:

Pointers can be used to alter variables they point to:

When a pointer is sent to a function by value, the function can access the memory addressed by the pointer and make changes to it:

void swap(int* a_ptr, int* b_ptr){
    int temp = *a_ptr;
    *a_ptr = b_ptr;             //memroy addressed by the pointers
    *b_ptr = temp;              //can be accessed and altered
}

It is true that a_ptr and b_ptr in this example are passed by value, but since they contain addresses of actual variables in the caller function, they can be used to access and alter the actual variables.

Function cannot change the address the pointer is pointing to:

And since a pass by value pointer is only the copy of the address of the variable, if the address in the copy changes, that does not change the value of the pointer used in the call. 

Consider the StrLen function for example:

int main(int argc, char *argv[])
{
    cout <<endl<<endl<< "=======================" << endl;

    char* str = new char[50];
    strcpy(str, "some string in the calling function.");

    int len = StrLen(str);
    cout<<"length of string: "<<len<<endl<<endl;

    cout<<"Let's see if str still points to the beginning of the string: "<<endl;
    cout<<"here is str: ["<<str<<"]"<<endl;

    cout <<endl<<endl<< "=======================" << endl;
    return 0;
}

int StrLen(char* s){
    int len = 0;
    for (;*s!=NULL;s++){    //s is changing. But since s is being passed
        len++;              //by value, it's only the copy that is changing
    }
    return len;
}

Here is the output of this little program:

=======================
length of string: 36

Let's see if str still points to the beginning of the string: 
here is str: [some string in the calling function.]


=======================

The fact that we can print str after calling StrLen shows us that the function has not changed the address stored in str in main. Even though the function is clearly changes the argument s inside the for loop.

 

Passing pointer By Reference:

 In some circumstances, we need the function to be able to change where the pointer argument is pointing to. Here is a bad example of that:

int main(int argc, char *argv[])
{
    cout <<endl<<endl<< "=======================" << endl;

    char* str;      //uninitialized pointer
    Allocate(str, 30);
    ....


    cout <<endl<<endl<< "=======================" << endl;
    return 0;
}
void Allocate(char* &s, int size){
    s = new char[size];
}

Think about the reason why this is not such a great example.

Here, the str pointer goes into the function uninitialized, but comes back pointing to a block of 30 characters.

 

Return pointers from functions:

This allocate function would be much more elegant if it were done the right way:

int main(int argc, char *argv[])
{
    cout <<endl<<endl<< "=======================" << endl;

    char* str;      //uninitialized pointer
    str = Allocate(30);
    ....


    cout <<endl<<endl<< "=======================" << endl;
    return 0;
}
char* Allocate(int size){
    char* s = new char[size];
    return s;
} 

Many of you have dreamt of being able to return arrays from functions. Now you can do just that. May all your wishes come true.