얼룩와와 2019. 10. 1. 08:21

resource: https://www.w3schools.com/cpp/cpp_pointers.asp

 

C++ Pointers

C++ Pointers Creating Pointers You learned from the previous chapter, that we can get the memory address of a variable by using the & operator: Example string food = "Pizza"; // A food variable of type string cout << food;  // Outputs the value of food (Pi

www.w3schools.com

"&" gets you memory address of variables

 

A pointer is a variable that stores memory address as its value.

 

A pointer is creted by "*" operator.

string food = "Pizza";  // A food variable of type string
string* ptr = &food;    // A pointer variable, with the name ptr, that stores the address of food

 

Operator "*" does two different things.

 

1) It creates a pointer variable

2) It gets also the value of the variable. 

 

string food = "Pizza";  // Variable declaration
string* ptr = &food;    // Pointer declaration

// Reference: Output the memory address of food with the pointer (0x6dfed4)
cout << ptr << "\n";

// Dereference: Output the value of food with the pointer (Pizza)
cout << *ptr << "\n";