Pointer Type

Basics

  • A derived type to store the memory address of an lvalue

  • * symbol after type to make a derived type BaseType *ptr;

  • * operator to dereference a pointer variable *ptr

  • & operator to reference an lvalue ptr = &lvalue;

Example

1int value = 10;
2int *ptr1;
3ptr1 = &value;  // reference the value, now you can use *ptr1 as an alias of value
4(*ptr1)++;  // value increased by 1 and become 11
5
6cout << "The value is " << (*ptr1) << endl;  // dereference
7
8// type cast to (void *) type to be displayed correctly
9cout << "The memory address is " << static_cast<void *>(ptr1) << endl;
  • Pitfalls

    • confusing pointer syntaxes with reference syntaxes

Pointer Type Based Data Structures

  • Linked-list

  • Tree

  • Graph

  • Hash Table (chained)