// On our second day we talked a lot about pointers. // @topic T00200 Second day lecture -- the concept of pointers // @brief Pointer is a data type #include <iostream> #include <string> using namespace std; // Function taking pointers as parameters: void indirect_demo( int* pvalue, string* pmessage ) { *pvalue = 456; *pmessage = "Goodbye"; (*pmessage).length(); pmessage->length(); // pointer-to-member deref... } // Various ways to printing characters: void print( char* pmessage ) { char ch = 'A'; cout << ch << '\n'; cout << *pmessage << '\n'; cout << pmessage << '\n'; cout << pmessage[ 2 ] << '\n'; // more on this next week!! } int main() { // First, we talked about integers and strings: int x = 123; int y = 789; // NULL is often used to initialize pointers: int* ptrx = NULL; // NULL macro iz defined as 0 (zero) // Pointer is a variable -- it can store addresses: ptrx = &x; // take address of x and store in ptrx ptrx = &y; // take address of y and store in ptrx string str = "Hello"; string str2; string* ptrstr = &str; // pointer to a string // Demonstrates passing addresses to a function which // expects pointers as parameters: indirect_demo( &x, &str ); indirect_demo( &y, &str2 ); // At the end of the class we started talking about pointers to characters: print( "Hello" ); return 0; }