1 / 25

Beginning C++ Through Game Programming, Second Edition

Beginning C++ Through Game Programming, Second Edition. by Michael Dawson. Chapter 7. Pointers: Tic-Tac-Toe 2.0. Objectives. Declare and initialize pointers Dereference pointers Use constants and pointers Pass and return pointers Work with pointers and arrays. Pointer Basics.

marvin
Download Presentation

Beginning C++ Through Game Programming, Second Edition

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Beginning C++ Through Game Programming, Second Edition by Michael Dawson

  2. Chapter 7 • Pointers: Tic-Tac-Toe 2.0

  3. Objectives • Declare and initialize pointers • Dereference pointers • Use constants and pointers • Pass and return pointers • Work with pointers and arrays

  4. Pointer Basics • A pointer is a variable that can contain a memory address • Pointers give you the ability to work directly and efficiently with memory • Like iterators, they're often used to access the contents of other variables

  5. Declaring Pointers • int* pAPointer; • Programmers often prefix pointer variable names with p • Declared to point to a specific type of value • pAPointer is a pointer to int; it can only point to an int value.

  6. Pointer Declaration Pitfalls • int* pScore, score; • score is not a pointer! It’s a variable of type int. • Clearer • int *pScore, score; • Clearest • int* pScore; • int score;

  7. Initializing Pointers • int* pScore = 0; • Assigning 0 to means “point to nothing” • Called a null pointer • Can use NULL instead of 0

  8. Assigning Addresses to Pointers • Assign pointer pScore address of variable score • pScore = &score;

  9. Dereferencing Pointers • Dereference a pointer to access the object to which it points • cout << *pScore; • Sends cout value that pScore points to • Don’t dereference a null pointer • Don't forget to dereference a pointer • pScore += 500; • Adds 500 to the address stored in pScore, not to the value to which pScore originally pointed

  10. Reassigning Pointers • Unlike references, pointers can point to different objects • pScore = &newScore; • Following doesn't change pScore, only the value to which it points • *pScore = newScore;

  11. Using Pointers to Objects • string str = "score"; • string* pStr = &str; • Can call member functions of an object through a pointer, just as with iterators • cout << (*pStr).size(); • Just as with iterators, you can use the -> operator with pointers for a more readable way to access object members • cout << pStr->size();

  12. Constant Pointer • int i = 100; • int* const p = &i; • Can only point to the object to which it was initialized to point • Can be used to change value it points to • Can only point to a non-constant value; can’t point to a constant.

  13. Pointer to a Constant • const int* p; • Can’t be used to change the value to which it points • Can point to different objects • Can point to a constant or non-constant value

  14. Constant Pointer to a Constant • cont int I = 100; • const int* const p = &I; • Can only point to the value to which it was initialized to point • Can’t be used to change the value to which it points • Can be initialized to point to a constant or a non-constant value

  15. Passing Pointers • Can pass access to variables using pointers • Just as with references • References are the preferred way to pass arguments

  16. Example: Passing Pointers • When you pass a pointer, you pass only the address of an object • Can be quite efficient, especially if you’re working with large objects • void Swap(int* const pX, int* const pY) • pX and pY are constant pointers and will each accept a memory address • Must pass addresses when passing to pointer parameters • Swap(&myScore, &yourScore);

  17. Returning Pointers • Can return pointer from a function • Preferred method is to return reference • Specify that you’re returning a pointer in function header • string* func() • Return an address in the function body • return &myString;

  18. Pitfalls of Return Pointers • Don't return a pointer that points to an out-of-scope object • Never return a pointer to a local variable • string* badPointer() • { • string local = "Won't exist."; • string* pLocal = &local; • return pLocal; • }

  19. Assignments and Returned Pointers • Assign the returned pointer to a pointer that points to an object of the same type • string* pStr = func(); • Efficient; assigning a pointer to a pointer, no string object is copied. • string str = func(); • Copies the string object that the returned pointers point to • For large objects, could be an expensive operation

  20. Pointers and Arrays • An array name is really a constant pointer to the first element of the array • Elements of an array are stored in a contiguous block of memory • Can use the array name as a pointer for random access to elements • Pass arrays as pointers

  21. Using Array Name as a Pointer • const int NUM_SCORES = 3; • int highScores[NUM_SCORES] = {5000, 3500, 2700}; • cout << *highScores << endl; • cout << *(highScores + 1) << endl; • cout << *(highScores + 2) << "\n\n"; • Output is as follows: • 5000 • 3500 • 2700

  22. Passing Array as a Pointer • void display(const int* const array, • const int NUM_ELEMENTS) • { • for (int i = 0; i < NUM_ELEMENTS; ++i) • cout << array[i] << endl; • } • Accepts array and number of elements • Displays all elements

  23. Summary • A pointer is a variable that contains a memory address • Pointers act like iterators from the STL • To declare a pointer, list a type, followed by an asterisk, followed by a name • A pointer is declared to hold memory address of a specific type • Good practice to initialize a pointer when you declare it

  24. Summary (cont.) • If you assign 0 to a pointer, the pointer is called a null pointer • To get the address of a variable, put the address of the operator (&) before the variable name • Unlike references, you can reassign pointers • Dereference a pointer to access the object it points to with *, the dereference operator • Use the -> operator with pointers for a more readable way to access object data members and member functions

  25. Summary (cont.) • A constant pointer can only point to the object it was initialized to point to • Can't use a pointer to a constant to change the value to which it points • A constant pointer to a constant can only point to the value it was initialized to point to, and it can’t be used to change that value • You can pass pointers for efficiency or to provide direct access to an object • A dangling pointer is a pointer to an invalid memory address • Dereferencing a dangling pointer can lead to disastrous results • You can return a pointer from a function, but be careful not to return a dangling pointer

More Related