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
27 trang |
Chia sẻ: thuongdt324 | Lượt xem: 485 | Lượt tải: 0
Bạn đang xem trước 20 trang tài liệu Electrical Engineering - Chapter 11: Pointer Variables, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
Chapter 11 – Pointer VariablesDeclaring a Pointer VariableDeclared with data type, * and identifier type* pointer_variable;* follows data typeUsually no space between so more obviousSpace allowed, so 1 or more would still workReserves space for storageMust initialize or assign addressLesson 11.1Assigning an AddressUse the "address of" operator (&)General form: pointer_variable = &ordinary_variableLesson 11.1Name of the pointerName of ordinary variableUsing a Pointer VariableCan be used to access a valueUnary operator * used * pointer_variableIn executable statement, indicates valueCommon practice to use suffix ptr for pointer variablesExample: width_ptrLesson 11.1Pointer OperatorsAmpersand &Address ofAsterisk *Get value at the address Lesson 11.1Uses 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 = 10.5;Lesson 11.1Uses of &Declaration of function header to indicate a reference void function1 (double&)In executable statement to indicate "address of" height_address = &height;Lesson 11.1Spacing a *Somewhat flexible in declaration or header double* height; double *height; double * height;Lesson 11.1Confusing – looks likemultiplication usageFor multiple pointer declarations double *height *width;double* height;double* width;orTransferring Addresses to FunctionsIn 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 11.2Using Pointer for Array TransferLesson 11.3rtype function (type*, int);function (array, num);rtype function (type* b, int num_elem) { code using b with brackets (b[n]) }DeclarationCallHeaderFunctionBodyExample:Lesson 11.3void function2 (double*, int);int main ( ){ double c[5] = {2.1, 3.5, 6.4, 1.9, 4.5}; function2 ( c, 5);} void function2 (double* b, int num_elem)Name of array is addressPointer Variables and MathDeclare variable as pointer double* b;Holds addressCan perform addition and subtractionPurpose of add is to advance to subsequent cell b + 1 (double is 8 bytes, so adds 8 bytes)Subtraction goes back to previous memory cell b – 1 (goes back 8 bytes)Lesson 11.4Returning Array Address from FunctionLesson 11.5Example: 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 usePointer VariablesPoint to 2, 4, or 8 bytes of memory depending on type of variableCan point to entire arrayHolds address of beginning of arrayPointer declaration indicates size of memory greater than single valueLesson 11.6Creating Pointer to ArraysExample: double (*f) [2];Lesson 11.6Declares f to be a pointerOne dimensional array of size 2Array elements of type doubleExample: double (*h) [3] [5];Declares h to be a pointerTwo dimensional array of size 3 * 5 (15 elements)Array elements of type double( ) are REQUIREDtypedef StatementUsed to create an alias for a data typeNote – not a new data typeBasic form typedef type synonym_1, synonym_n;Lesson 11.6Any valid data typeList of valid identifiers (any number)Uses of typedefImprove readability and understandability of the codeEasier to modify programs that are implementation dependentLesson 11.6Arrays and typedefGeneral form: typedef type name [size];type is the type of values in the arrayname is the alias to be used as the data type in a declarationsize is array sizeLesson 11.6typedef for Pointer to ArrayGeneral form: typedef type (*name) [size];type is type of values in arrayname is the alias to be used as data type in declarationsize is array sizeLesson 11.6Returning a PointerGiven the example: typedef double (*array_ptr) [2];Creates alias array_ptr for declarations of pointers to 1-D arrays of double with size 2array_ptr function2 (argument);Indicates pointer to 1-D array is returned from functionLesson 11.6Multidimensional ArraysC++ sees array of arraysExample: int a[2] [3] [4];Can work with whole (or 1) arrayCan use 2 arrays of [3] [4]Can use 6 arrays of [4]Can use 24 integersPointers: int (*b)[3] [4], (*c)[4], *d, aCan assign addresses with & operator ( b = &a[0]; )Lesson 11.6Pointers to ObjectsSpecial arrow operator ->Negative and greater than symbol with no space betweenUsed to access members with object's addressDeclaring pointer Class_name* ptr_name;Initializing pointer ptr_name = &object_name;Calling member function (2 argument example) ptr_name -> function_name (arg1, arg2)Lesson 11.7Pointers as Data MembersGeneral form class Class_name { private: type* pointer_name; public: type function_name ( ); };Lesson 11.8Name of the classAny valid data type including name of struct or className of member functionDynamic Memory AllocationOptimize memory space with new and delete operatorsReserve and unreserve memory while program is executionPointer variables important because operators work with addresses of memory reservedLesson 11.9new OperatorGeneral form: new type [num_elements]type is data type of array elementsnum_elements is number of array elementsReserves memory but does NOT fill with valuesnew returns address of memory it reservesValue assigned to pointer variable of same typeLesson 11.9 type* array_address; array_address = new type [num]; delete OperatorGeneral form: delete [ ] array_address;array_address is pointer variableReleases memory stored at address indicatedKnows size of memory reserved by new and releases memory for all the arraydelete address; deletes memory for single elementLesson 11.9SummaryDeclare and initialize pointer variablesPass addresses to functionsReturn an address from a functionReserve memory during executionLink classes with accessor functionsLearned how to: