Xem mẫu
1
Chapter 5 - 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 constwith 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 String-
Handling Library
2003 Prentice Hall, Inc. All rights reserved.
Pointer Variable Declarations 2 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)
• Indirection
countPtr count
7
– 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.
Pointer Variable Declarations and 3 Initialization
• Can declare pointers to any data type • Pointer initialization
– Initialized to 0, NULL, or address •0or NULLpoints 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
yPtr 5 500000 600000
y
600000 5
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 –*yPtrreturns y(because yPtrpoints 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.
...
- tailieumienphi.vn
nguon tai.lieu . vn