Show Menu
Cheatography

C++ references and pointers in function calls Cheat Sheet by

C++ references and pointers in function calls - passing as argument

Regular variable

// variable definition

int count = 10;
// function definitions
void example( int i) {     cout << i;     i = 30; }
int main() {     example(count);     cout << count;     return 0; }
// terminal output
10 10
 

Reference

// variable definition

int count = 10;
// functions definition
void example( int& i) {     cout << i;     i = 30; }
int main() {     example(count);     cout << count;     return 0; }
// terminal output
10 30
 

Pointer

// variable definition

int count = 10;
// pointer definition
int* pCount = &count;
//functions definition
void example( int* i) {     cout << *i;     *i = 30; }
int main() {     example(pCount);     cout << count;     return 0; }
// terminal output
10 30
   
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          C Reference Cheat Sheet
          C++ Pointers cookbook Cheat Sheet