Giáo trình tin học Chương V

48 514 3
Giáo trình tin học Chương V

Đ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

Giáo trình tin học chương V

1 Chapter - Pointers and Strings Outline 5.1 5.2 5.3 5.4 5.5 5.6 5.7 5.8 5.9 5.10 5.11 Introduction Pointer Variable Declarations and Initialization Pointer Operators Calling Functions by Reference Using const with Pointers Bubble Sort Using Pass-by-Reference Pointer Expressions and Pointer Arithmetic Relationship Between Pointers and Arrays Arrays of Pointers Function Pointers Introduction to Character and String Processing 5.11.1 Fundamentals of Characters and Strings 5.11.2 String Manipulation Functions of the StringHandling Library  2003 Prentice Hall, Inc All rights reserved Pointer Variable Declarations and Initialization • Pointer variables – Contain memory addresses as values – Normally, variable contains specific value (direct reference) – Pointers contain address of variable that has specific value (indirect reference) countPtr count • 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 count Pointer Variable Declarations and Initialization • Can declare pointers to any data type • Pointer initialization – Initialized to 0, NULL, or address • or NULL points to nothing  2003 Prentice Hall, Inc All rights reserved 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 yPtr y y yptr 500000 600000 600000 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 to y • * and & are inverses of each other  2003 Prentice Hall, Inc All rights reserved 10 11 13 14 16 17 19 20 22 23 24 26 28 #include using std::cout; using std::endl; int main() { int a; // a is an integer int *aPtr; // aPtr is a pointer to an integer a = 7; aPtr = &a; // aPtr assigned address of a cout

Ngày đăng: 22/08/2012, 10:13

Từ khóa liên quan

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

Tài liệu liên quan