Learning materials

Pointer Type

On this page

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

int value = 10;
int *ptr1;
ptr1 = &value;  // reference the value, now you can use *ptr1 as an alias of value
(*ptr1)++;  // value increased by 1 and become 11

cout << "The value is " << (*ptr1) << endl;  // dereference

// type cast to (void *) type to be displayed correctly
cout << "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)