# data[].attributes.body
•Tokenizing

–Breaking strings into tokens, separated by delimiting characters

–Tokens usually logical units, such as words (separated by spaces)

–"This is my string" has 4 word tokens (separated by spaces)

–char *strtok( char *s1, const char *s2 )

# data[].attributes.headline
String Manipulation Functions of the String-handling Library

# data[].attributes.subject
String Manipulation Functions of the String-handling Library

# data[].attributes.body
•Copying strings

–char *strcpy( char *s1, const char *s2 )

•Copies second argument into first argument

–First argument must be large enough to store string and terminating null character

–char *strncpy( char *s1, const char *s2,      size_t n )

•Specifies number of characters to be copied from string into array

•Does not necessarily copy terminating null character

# data[].attributes.headline
String Manipulation Functions of the String-handling Library

# data[].attributes.subject
String Manipulation Functions of the String-handling Library

# data[].attributes.body
•Character constant

–Integer value represented as character in single quotes

–'z' is integer value of z

•122 in ASCII

•String

–Series of characters treated as single unit

–Can include letters, digits, special characters  +, -, * ...

–String literal (string constants)

•Enclosed in double quotes, for example:

# data[].attributes.body
•Pointers to functions

–Contain address of function

–Similar to how array name is address of first element

–Function name is starting address of code that defines function

•Function pointers can be

–Passed to functions

–Returned from functions

–Stored in arrays

–Assigned to other function pointers

# data[].attributes.body
•Arrays can contain pointers

–Commonly used to store array of strings

char *suit[ 4 ] = {"Hearts", "Diamonds",
"Clubs", "Spades" };

–Each element of suit points to char * (a string)

–Array does not store strings, only pointers to strings

# data[].attributes.body
•Arrays and pointers closely related

–Array name like constant pointer

–Pointers can do array subscripting operations

•Accessing array elements with pointers

–Element b[ n ] can be accessed by  *( bPtr + n )

•Called pointer/offset notation

–Addresses

•&b[ 3 ] same as bPtr + 3

–Array name can be treated as pointer

•b[ 3 ] same as *( b + 3 )

–Pointers can be subscripted (pointer/subscript notation)

•bPtr[ 3 ] same as b[ 3 ]

# data[].attributes.headline
Relationship Between Pointers and Arrays

# data[].attributes.subject
Relationship Between Pointers and Arrays

# data[].attributes.body
•Pointer arithmetic

–Increment/decrement pointer  (++ or --)

–Add/subtract an integer to/from a pointer( + or += , - or -=)

–Pointers may be subtracted from each other

–Pointer arithmetic meaningless unless performed on pointer to array

•5 element int array on a machine using 4 byte ints

–vPtr points to first element v[ 0 ], which is at location 3000

vPtr = 3000

–vPtr += 2; sets vPtr to 3008

vPtr points to v[ 2 ]

# data[].attributes.headline
Pointer Expressions and Pointer Arithmetic

# data[].attributes.subject
Pointer Expressions and Pointer Arithmetic

# data[].attributes.body
•sizeof

–Unary operator returns size of operand in bytes

–For arrays, sizeof returns

( size of 1 element ) * ( number of elements )

–If sizeof( int ) = 4, then

intmyArray[10];

cout << sizeof(myArray);

will print 40

•sizeof can be used with

–Variable names

–Type names

–Constant values

# data[].attributes.body
•Implement bubbleSort using pointers

–Want function swap to access array elements

•Individual array elements: scalars

–Passed by value by default

•Pass by reference using address operator &

# data[].attributes.body
•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

# data[].attributes.body
•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”

# data[].attributes.body
•* (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

# data[].attributes.body
•& (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

# data[].attributes.body
•Can declare pointers to any data type

•Pointer initialization

–Initialized to 0, NULL, or address

•0 or NULL points to nothing

# data[].attributes.headline
Pointer Variable Declarations and Initialization

# data[].attributes.subject
Pointer Variable Declarations and Initialization

# data[].attributes.body
•Pointers

–Powerful, but difficult to master

–Simulate pass-by-reference

–Close relationship with arrays and strings