{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

pointer - 1 Chapter 5 Pointers and Strings Outline 5.1 5.2...

Info icon This preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
1 2000 Deitel & Associates, Inc. All rights reserved. Chapter 5 - Pointers and Strings Outline 5.1 Introduction 5.2 Pointer Variable Declarations and Initialization 5.3 Pointer Operators 5.4 Calling Functions by Reference 5.5 Using the Const Qualifier with Pointers 5.6 Bubble Sort Using Call-by-reference 5.7 Pointer Expressions and Pointer Arithmetic 5.8 The Relationship Between Pointers and Arrays 5.9 Arrays of Pointers 5.10 Case Study: A Card Shuffling and Dealing Simulation 5.11 Function Pointers 5.12 Introduction to Character and String Processing 5.12.1 Fundamentals of Characters and Strings 5.12.2 String Manipulation Functions of the String-handling Library 5.13 Thinking About Objects: Interactions Among Objects  
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
2 2000 Deitel & Associates, Inc. All rights reserved. 5.1 Introduction Pointers Powerful, but difficult to master Simulate call-by-reference Close relationship with arrays and strings
Image of page 2
3 2000 Deitel & Associates, Inc. All rights reserved. 5.2 Pointer Variable Declarations and  Initialization Pointer variables Contain memory addresses as their values Normal variables contain a specific value (direct reference) Pointers contain the address of a variable that has a specific value (indirect reference) Indirection Referencing a pointer value Pointer declarations * indicates variable is a pointer int *myPtr; declares a pointer to an int , a pointer of type int * Multiple pointers require multiple asterisks int *myPtr1, *myPtr2; count 7 countPtr count 7
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
4 2000 Deitel & Associates, Inc. All rights reserved. 5.2 Pointer Variable Declarations and  Initialization Can declare pointers to any data type Pointers initialization Initialized to 0 , NULL , or an address 0 or NULL points to nothing
Image of page 4
5 2000 Deitel & Associates, Inc. All rights reserved. 5.3 Pointer Operators & (address operator) Returns the address of its operand Example int y = 5; int *yPtr; yPtr = &y; // yPtr gets address of y yPtr “points to” y yPtr y 5 yptr 500000 600000 y 600000 5 address of y is value of yptr
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
6 2000 Deitel & Associates, Inc. All rights reserved. 5.3 Pointer Operators * (indirection/dereferencing operator) Returns the value of what its operand points to *yPtr returns y (because yPtr points to y ). * can be used to assign a value to a location in memory *yptr = 7; // changes y to 7 Dereferenced pointer (operand of * ) must be an lvalue (no constants) * and & are inverses Cancel each other out *&myVar == myVar and &*yPtr == yPtr
Image of page 6
2000 Deitel & Associates, Inc. All rights reserved. Outline 1.  Declare variables 2  Initialize variables 3.  Print Program Output 1 // Fig. 5.4: fig05_04.cpp 2 // Using the & and * operators 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 8 int main() 9 { 10 int a; // a is an integer 11 int *aPtr; // aPtr is a pointer to an integer 12 13 a = 7; 14 aPtr = &a; // aPtr set to address of a 15 16 cout << "The address of a is " << &a 17 << "\nThe value of aPtr is " << aPtr; 18 19 cout << "\n\nThe value of a is " << a 20 << "\nThe value of *aPtr is " << *aPtr; 21 22 cout << "\n\nShowing that * and & are inverses of " 23 << "each other.\n&*aPtr = " << &*aPtr
Image of page 7

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern