Overview

Why use pointers?

What's a pointer?

Declaring pointers

Initializing/Setting pointers

Pointer operators

Pointer arithmetic

Pointers as parameters

Pointers vs. arrays

Array of pointers

Function pointers

 


Pointers as function parameters

  • declare a function with parameters of pointers using (*) operator
  • pass address of variable when calling a function
  • function can modify the object the pointer argument references

Example:

void square(int *y);

main(){

    int score = 10;

    cout << score;    // output is 10
    square(&score);
    cout << score;    // output is 100
}

void square(int *y)
{
    *y= (*y) * (*y);
}