15.2 Pointers and Printing

I l @ ve RuBoard

In C++ you can display the value of a pointer just like you can display the value of a simple variable such as an integer or floating point number. For example:

 int an_integer = 5;           // A simple integer int *int_ptr = &an_integer;   // Pointer to an integer std::cout << "Integer pointer " << int_ptr << '\n'; 

outputs

 Integer pointer 0x58239A 

In this case, the value 0x58239A represents a memory address. This address may vary from program to program.

C++ treats character pointers a little differently from other pointers. A character pointer is treated as a pointer to a C-style string. For example:

 char some_characters[10] = "Hello";   // A simple set of characters char *char_ptr = &some_characters[0];  // Pointer to a character std::cout << "String pointer " << char_ptr << '\n'; 

outputs

 String pointer Hello 

So with string pointers, the string itself is printed.

I l @ ve RuBoard


Practical C++ Programming
Practical C Programming, 3rd Edition
ISBN: 1565923065
EAN: 2147483647
Year: 2003
Pages: 364

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net