Giáo trình tin học chương 5
Số trang: 48
Loại file: pdf
Dung lượng: 191.75 KB
Lượt xem: 17
Lượt tải: 0
Xem trước 5 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Using const with Pointers• const qualifier– Value of variable should not be modified– const used when function does not need to change a variable• Principle of least privilege– Award function enough access to accomplish task, but no more• Four ways to pass pointer to function– Nonconstant pointer to nonconstant data• Highest amount of access– Nonconstant pointer to constant data– Constant pointer to nonconstant data– Constant pointer to constant data• Least amount of access...
Nội dung trích xuất từ tài liệu:
Giáo trình tin học chương 5 1 Chapter 5 - Pointers and Strings Outline 5.1 Introduction 5.2 Pointer Variable Declarations and Initialization 5.3 Pointer Operators 5.4 Calling Functions by Reference 5.5 Using const with Pointers 5.6 Bubble Sort Using Pass-by-Reference 5.7 Pointer Expressions and Pointer Arithmetic 5.8 Relationship Between Pointers and Arrays 5.9 Arrays of Pointers 5.10 Function Pointers 5.11 Introduction to Character and String Processing 5.11.1 Fundamentals of Characters and Strings 5.11.2 String Manipulation Functions of the String- Handling Library 2003 Prentice Hall, Inc. All rights reserved. 2 Pointer Variable Declarations and Initialization • Pointer variables – Contain memory addresses as values count – Normally, variable contains specific value (direct reference) 7 – Pointers contain address of variable that has specific value (indirect reference) countPtr count 7 • Indirection – Referencing value through pointer • Pointer declarations – * indicates variable is pointer int *myPtr; declares pointer to int, pointer of type int * – Multiple pointers require multiple asterisks int *myPtr1, *myPtr2; 2003 Prentice Hall, Inc. All rights reserved. 3 Pointer Variable Declarations and Initialization • Can declare pointers to any data type • Pointer initialization – Initialized to 0, NULL, or address • 0 or NULL points to nothing 2003 Prentice Hall, Inc. All rights reserved. 4 Pointer Operators • & (address operator) – Returns memory address of its operand – Example int y = 5; int *yPtr; yPtr = &y; // yPtr gets address of y – yPtr “points to” y y yptr y 5 500000 600000 600000 5 yPtr address of y is value of yptr 2003 Prentice Hall, Inc. All rights reserved. 5 Pointer Operators • * (indirection/dereferencing operator) – Returns synonym for object its pointer operand points to – *yPtr returns y (because yPtr points to y). – dereferenced pointer is lvalue *yptr = 9; // assigns 9 to y • * and & are inverses of each other 2003 Prentice Hall, Inc. All rights reserved.3 #include 65 using std::cout;6 using std::endl;8 int main() {10 int a; // a is an integer11 int *aPtr; // aPtr is a pointer to an integer13 a = 7;14 aPtr = &a; // aPtr assigned address of a16 cout 7 Calling Functions by Reference• 3 ways to pass arguments to function – Pass-by-value – Pass-by-reference with reference arguments – Pass-by-reference with pointer arguments• return can return one value from function• Arguments passed to function using reference arguments – Modify original values of arguments – More than one value “returned”• Pass-by-reference with pointer arguments – Simulate pass-by-reference • Use pointers and indirection operator – Pass address of argument using & operator – Arrays not passed with & because array name already pointer – * operator used as alias/nickname for variable inside of 2003 Prentice Hall, Inc. All rights reserved. 84 #include 6 using std::cout;7 using std::endl;9 void cubeByReference( int * ); // prototype11 int main() {13 int number = 5;15 cout 9 Using const with Pointers • const qualifier – Value of variable should not be modified – const used when function does not need to change a variable • Principle of least privilege – Award function enough access to accomplish task, but no more • Four ways to pass pointer to function – Nonconstant pointer to nonconstant data • Highest amount of access – Nonconstant pointer to constant data – Constant pointer to nonconstant data – Constant pointer to constant ...
Nội dung trích xuất từ tài liệu:
Giáo trình tin học chương 5 1 Chapter 5 - Pointers and Strings Outline 5.1 Introduction 5.2 Pointer Variable Declarations and Initialization 5.3 Pointer Operators 5.4 Calling Functions by Reference 5.5 Using const with Pointers 5.6 Bubble Sort Using Pass-by-Reference 5.7 Pointer Expressions and Pointer Arithmetic 5.8 Relationship Between Pointers and Arrays 5.9 Arrays of Pointers 5.10 Function Pointers 5.11 Introduction to Character and String Processing 5.11.1 Fundamentals of Characters and Strings 5.11.2 String Manipulation Functions of the String- Handling Library 2003 Prentice Hall, Inc. All rights reserved. 2 Pointer Variable Declarations and Initialization • Pointer variables – Contain memory addresses as values count – Normally, variable contains specific value (direct reference) 7 – Pointers contain address of variable that has specific value (indirect reference) countPtr count 7 • Indirection – Referencing value through pointer • Pointer declarations – * indicates variable is pointer int *myPtr; declares pointer to int, pointer of type int * – Multiple pointers require multiple asterisks int *myPtr1, *myPtr2; 2003 Prentice Hall, Inc. All rights reserved. 3 Pointer Variable Declarations and Initialization • Can declare pointers to any data type • Pointer initialization – Initialized to 0, NULL, or address • 0 or NULL points to nothing 2003 Prentice Hall, Inc. All rights reserved. 4 Pointer Operators • & (address operator) – Returns memory address of its operand – Example int y = 5; int *yPtr; yPtr = &y; // yPtr gets address of y – yPtr “points to” y y yptr y 5 500000 600000 600000 5 yPtr address of y is value of yptr 2003 Prentice Hall, Inc. All rights reserved. 5 Pointer Operators • * (indirection/dereferencing operator) – Returns synonym for object its pointer operand points to – *yPtr returns y (because yPtr points to y). – dereferenced pointer is lvalue *yptr = 9; // assigns 9 to y • * and & are inverses of each other 2003 Prentice Hall, Inc. All rights reserved.3 #include 65 using std::cout;6 using std::endl;8 int main() {10 int a; // a is an integer11 int *aPtr; // aPtr is a pointer to an integer13 a = 7;14 aPtr = &a; // aPtr assigned address of a16 cout 7 Calling Functions by Reference• 3 ways to pass arguments to function – Pass-by-value – Pass-by-reference with reference arguments – Pass-by-reference with pointer arguments• return can return one value from function• Arguments passed to function using reference arguments – Modify original values of arguments – More than one value “returned”• Pass-by-reference with pointer arguments – Simulate pass-by-reference • Use pointers and indirection operator – Pass address of argument using & operator – Arrays not passed with & because array name already pointer – * operator used as alias/nickname for variable inside of 2003 Prentice Hall, Inc. All rights reserved. 84 #include 6 using std::cout;7 using std::endl;9 void cubeByReference( int * ); // prototype11 int main() {13 int number = 5;15 cout 9 Using const with Pointers • const qualifier – Value of variable should not be modified – const used when function does not need to change a variable • Principle of least privilege – Award function enough access to accomplish task, but no more • Four ways to pass pointer to function – Nonconstant pointer to nonconstant data • Highest amount of access – Nonconstant pointer to constant data – Constant pointer to nonconstant data – Constant pointer to constant ...
Tìm kiếm theo từ khóa liên quan:
công nghệ thông tin tin học văn phòng giáo trình word máy tính microsoft office pointers stringsGợi ý tài liệu liên quan:
-
73 trang 425 2 0
-
52 trang 414 1 0
-
Giáo trình Tin học văn phòng: Phần 2 - Bùi Thế Tâm
65 trang 301 0 0 -
Nhập môn Tin học căn bản: Phần 1
106 trang 300 0 0 -
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 296 0 0 -
Báo cáo thực tập thực tế: Nghiên cứu và xây dựng website bằng Wordpress
24 trang 287 0 0 -
74 trang 280 0 0
-
96 trang 280 0 0
-
Giáo trình Tin học MOS 1: Phần 1
58 trang 271 0 0 -
Tài liệu dạy học môn Tin học trong chương trình đào tạo trình độ cao đẳng
348 trang 267 1 0