1 of 13

Data Structures and Pointers

2 of 13

Agenda

  • Data Structures
  • Pointers

2

3 of 13

1.

Data Structures

Clumping stuff together

4 of 13

Data Structures

  • A data structure in a programming language allows us to glue together different types and treat them as a single object
  • Why do this?

4

5 of 13

Data Structures

  • Consider a car - it has:
    • Tires
    • Windows
    • Steering wheel
  • In our code we want to just have a single object for each car
  • Operating on tires, we know that they belong to this car

5

6 of 13

Data Structures

  • In C++ use the struct keyword

6

7 of 13

Data Structures

  • In Python, there is no support for direct data structures
  • Instead use classes
    • Only have simplified classes for this course
  • L12 Classes Python

7

8 of 13

2.

Pointers

Pointers going to point

9 of 13

Pointers

  • Variables are stored in computer memory
  • A pointer is a special kind of variable that indicates memory locations
  • Pointers exist naturally in C and C++
  • Pointers can be mimicked in Python

9

10 of 13

Why Use Pointers

  • Efficiency
    • Instead of large memory copies into functions have less expensive pointer copies
    • Very important with algorithms and data structures
  • Function variable modification
    • Modify external data from within a function
    • Can use global variables instead

10

11 of 13

Pointers in Python

  • Can store objects in arrays/lists and then use indices to the array as a pointer
  • Keep the objects as None (Python) until assigned values
  • More on the use of these faux pointers during data structures like trees

11

12 of 13

Pointers in C++

  • Pointers get types like other variables
  • A pointer has an asterisk * in front of the variable name
  • Using * in code finds the value of the pointer (at that memory location)
  • Using & for a regular variable gets the address of the variable
  • L12 Pointers in C++

12

13 of 13

CREDITS

Special thanks to all the people who made and released these awesome resources for free:

  • Presentation template by SlidesCarnival

13