C++ lecture 16

14 7 0
C++ lecture 16

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

C++ Programming Lecture 16 Pointers – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides) Outline       Introduction Pointer variables Pointer operators Calling functions by reference Using const with pointers Examples The Hashemite University Introduction   A pointer is a variable that contains a memory address Pointers     Powerful, but difficult to master Simulate call-by-reference Allow the creation of dynamic data structures that shrink and grow in size during run time, such as stacks, link lists, etc Close relationship with arrays and strings The Hashemite University Pointer Variable Declarations and Initialization I  Pointer variables     Indirection (the act of using a pointer)   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) Referencing a pointer value (accessing the contents of the memory location indicated by the address found inside the pointer) Pointer declarations  * indicates that a variable is a pointer int *myPtr; // this statement means create a pointer that points to an integer data  declares a pointer to an int, a pointer of type int * Multiple pointers require multiple asterisks int *myPtr1, *myPtr2; The Hashemite University Pointer Variable Declarations and Initialization II   Can declare pointers to any data type Pointers initialization  Initialized to 0, NULL, or an address  or NULL points to nothing Suppose that you defined x as  float x = 3.5; You have two options to declare a pointer that points to x: 1- float *pt = &x; (now pt is a pointer to x) 2- float *pt; pt = &x; (now pt is a pointer to x) The Hashemite University Pointer Operators I  & (address operator)     Returns the address of its operand It is different from the reference operator used in declaring reference variables Can be applied to variables only (cannot be applied to expressions or constants  syntax error) Example int y = 5; int *yPtr; yPtr = &y;  // yPtr gets address of y yPtr “points to” y yPtr y yptr 500000 600000 y 600000 address of y is value of The Hashemite University yptr Pointer Operators II  * (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 Dereferenced pointer (operand of *) must be an lvalue (no constants) * and & are inverses  Cancel each other out *&myVar == myVar and &*yPtr == yPtr The Hashemite University Example int f = 300; int *pt = &f; cout

Ngày đăng: 12/10/2021, 21:09

Mục lục

    C++ Programming Lecture 16 Pointers – Part I

    Pointer Variable Declarations and Initialization I

    Pointer Variable Declarations and Initialization II

    Using the const Qualifier with Pointers I

    Using the const Qualifier with Pointers II

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan