/*
* @topic Q00405 2/6/2014 C strings, addresses, and pointers
* @brief in-class demo -- function taking pointer as a parameter
*/
#include <iostream>
void pause()
{
//----------------------------------------------
// prevent window from closing before exiting:
//----------------------------------------------
char ch; // ch is a variable that holds a character
// curious \n is a "line feed" control character,
// it moves the cursor to the next line:
std::cout << "\nEnter X to Exit: ";
std::cin.ignore( 1, '\n' ); // extract and discard characters
std::cin.clear(); // this clears cin state
std::cin.get( ch ); // this reads a character from the keyboard,
// and pauses the program from before exiting
}
// This function expects an address of a character
// The function has a return type "void" which indicates
// that it does not return any value.
void print_message( char* msg )
{
// cout prints all characters at the given address
// until it finds NULL character at the end of the string:
std::cout << msg << '\n';
}
// This function expects an address of an integer
// The function has a return type "void" which indicates
// that it does not return any value.
void print_message( int* ptr_int )
{
// When cout sees an address of an integer, it prints
// the address as a hexadecimal number:
std::cout << ptr_int << '\n';
}
int main()
{
// call function that expects a pointer to a character:
print_message( "HELLO, FRIENDS!" );
int value = 100;
// call function that expects a pointer to an integer:
print_message( &value );
double amt = 10.99;
char newline = '\n';
amt = amt * value;
// Example that demonstrates that "Hello" returns an addresss
// of its first character (H) in memory:
int address_of_hello = (int) "Hello";
// std::hex signals that we want to print address_of_hello as
// a hexadecimal number:
std::cout << "Hello" << ' ' << std::hex << address_of_hello << newline;
std::cout << value << newline;
std::cout << amt << newline;
pause();
}