tailieunhanh - Lecture Programming in C++ - Chapter 11: Pointer variables
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. | Chapter 11 – Pointer Variables Declaring a Pointer Variable Declared 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 Assigning an Address Use the "address of" operator (&) General form: pointer_variable = &ordinary_variable Lesson Name of the pointer Name of ordinary variable Using a Pointer Variable Can be used to access a value Unary operator * used * pointer_variable In executable statement, indicates value Common practice to use suffix ptr for pointer variables Example: width_ptr Lesson Pointer Operators Ampersand & Address of Asterisk * Get value at the address Lesson Uses of * Binary multiplication operator volume = height * depth * width; Declaration specifier indicating address to be stored in variable's memory cell double* height_address; Unary operator indicating to . | Chapter 11 – Pointer Variables Declaring a Pointer Variable Declared 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 Assigning an Address Use the "address of" operator (&) General form: pointer_variable = &ordinary_variable Lesson Name of the pointer Name of ordinary variable Using a Pointer Variable Can be used to access a value Unary operator * used * pointer_variable In executable statement, indicates value Common practice to use suffix ptr for pointer variables Example: width_ptr Lesson Pointer Operators Ampersand & Address of Asterisk * Get value at the address Lesson Uses of * Binary multiplication operator volume = height * depth * width; Declaration specifier indicating address to be stored in variable's memory cell double* height_address; Unary operator indicating to get value or access memory cells at addressed *height_address = ; Lesson Uses of & Declaration of function header to indicate a reference void function1 (double&) In executable statement to indicate "address of" height_address = &height; Lesson Spacing a * Somewhat flexible in declaration or header double* height; double *height; double * height; Lesson Confusing – looks like multiplication usage For multiple pointer declarations double *height *width; double* height; double* width; or Transferring Addresses to Functions In declaration and header use type* in argument list type function (type*, type*); type funcName (type* name, type* name) In function call use &variable in argument list to pass address identifier = funcName (&name1, &name2); Lesson Using Pointer for Array Transfer Lesson rtype function (type*, int); function (array, num); rtype function (type* b, int num_elem) { code using b with brackets (b[n]) } Declaration Call Header .
đang nạp các trang xem trước