I l@ve RuBoard Previous Section Next Section

15.2 Pointers and Printing

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 Previous Section Next Section