Chapter 10 Pointers and Dynamic Arrays

The "address of" operator Also used to specify call-by-reference parameter. No coincidence!, Recall: call-by-reference parameters pass "address of" the actual argument | Chapter 10 Pointers and Dynamic Arrays Learning Objectives Pointers Pointer variables Memory management Dynamic Arrays Creating and using Pointer arithmetic Classes, Pointers, Dynamic Arrays The this pointer Destructors, copy constructors Pointer Introduction Pointer definition: Memory address of a variable Recall: memory divided Numbered memory locations Addresses used as name for variable You’ve used pointers already! Call-by-reference parameters Address of actual argument was passed Pointer Variables Pointers are "typed" Can store pointer in variable Not int, double, etc. Instead: A POINTER to int, double, etc.! Example: double *p; p is declared a "pointer to double" variable Can hold pointers to variables of type double Not other types! Declaring Pointer Variables Pointers declared like other types Add "*" before variable name Produces "pointer to" that type "*" must be before each variable int *p1, *p2, v1, v2; p1, p2 hold pointers to int variables v1, v2 are ordinary int . | Chapter 10 Pointers and Dynamic Arrays Learning Objectives Pointers Pointer variables Memory management Dynamic Arrays Creating and using Pointer arithmetic Classes, Pointers, Dynamic Arrays The this pointer Destructors, copy constructors Pointer Introduction Pointer definition: Memory address of a variable Recall: memory divided Numbered memory locations Addresses used as name for variable You’ve used pointers already! Call-by-reference parameters Address of actual argument was passed Pointer Variables Pointers are "typed" Can store pointer in variable Not int, double, etc. Instead: A POINTER to int, double, etc.! Example: double *p; p is declared a "pointer to double" variable Can hold pointers to variables of type double Not other types! Declaring Pointer Variables Pointers declared like other types Add "*" before variable name Produces "pointer to" that type "*" must be before each variable int *p1, *p2, v1, v2; p1, p2 hold pointers to int variables v1, v2 are ordinary int variables Addresses and Numbers Pointer is an address Address is an integer Pointer is NOT an integer! Not crazy abstraction! C++ forces pointers be used as addresses Cannot be used as numbers Even though it "is a" number Pointing Terminology, view Talk of "pointing", not "addresses" Pointer variable "points to" ordinary variable Leave "address" talk out Makes visualization clearer "See" memory references Arrows Pointing to int *p1, *p2, v1, v2; p1 = &v1; Sets pointer variable p1 to "point to" int variable v1 Operator, & Determines "address of" variable Read like: "p1 equals address of v1" Or "p1 points to v1" Pointing to Recall: int *p1, *p2, v1, v2; p1 = &v1; Two ways to refer to v1 now: Variable v1 itself: cout << v1; Via pointer p1: cout *p1; Dereference operator, * Pointer variable "derereferenced" Means: "Get data that p1 points to" "Pointing to" Example Consider: v1 = 0; p1 = &v1; *p1 = 42; cout << v1 << endl; cout << *p1 << endl; Produces output: 42 42 p1 and v1 refer to same

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU MỚI ĐĂNG
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.