On completion of this chapter students will know how to: Declare and initialize pointer variables, pass addresses to functions, return an address from a function, reserve memory during execution, link classes with accessor functions.
Trang 1Chapter 11 – Pointer Variables
Trang 2Declared with data type, * and identifier type* pointer_variable;
* follows data type
– Usually no space between so more obvious – Space allowed, so 1 or more would still work
Reserves space for storage
Must initialize or assign address
Lesson 11.1
Trang 4– Example: width_ptr
Lesson 11.1
Trang 6Lesson 11.1
Trang 7Declaration of function header to indicate a reference
Trang 8Somewhat flexible in declaration or header
Trang 10Header
Function
Body
Trang 12Lesson 11.4
Trang 13from Function
Lesson 11.5
Example: Function declaration and header double* get_array_address (double [ ] ); double* get_array_address (double c[ ] )
Now a return statement with variable thatindicates address in the function body
return c;
double*
Could also use
Trang 14Lesson 11.6
Trang 18– size is array size
Lesson 11.6
Trang 19– size is array size
Lesson 11.6
Trang 20Lesson 11.6
Trang 22Lesson 11.7
Trang 24Lesson 11.9
Trang 26– deletes memory for single element
Lesson 11.9
Trang 27Declare and initialize pointer variablesPass addresses to functions
Return an address from a function
Reserve memory during execution
Link classes with accessor functions
Learned how to: