Search, Char Array, Pointers
Search, Char Array, Pointers
Search, Char Array, Pointers
Programming
Lecture #9
Instructor: Jahan Zeb
Department of Computer Engineering (DCE)
College of E&ME
Searching Arrays: Linear Search
Multiple subscripts
– a[ i ][ j ]
– Tables with rows and columns
– Specify row, then column
– “Array of arrays”
• a[0] is an array of 4 elements
• a[0][0] is the first element of that array
Column 0 Column 1 Column 2 Column 3
Row 0 a[ 0 ][ 0 ] a[ 0 ][ 1 ] a[ 0 ][ 2 ] a[ 0 ][ 3 ]
Row 1 a[ 1 ][ 0 ] a[ 1 ][ 1 ] a[ 1 ][ 2 ] a[ 1 ][ 3 ]
Row 2 a[ 2 ][ 0 ] a[ 2 ][ 1 ] a[ 2 ][ 2 ] a[ 2 ][ 3 ]
Column subscript
Array name
Row subscript
Multiple-Subscripted Arrays
To initialize
– Default of 0
– Initializers grouped by row in braces
1 2
int b[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } }; 3 4
Row 0 Row 1
1 0
int b[ 2 ][ 2 ] = { { 1 }, { 3, 4 } }; 3 4
Multiple-Subscripted Arrays
– Outputs 0 3 4
Strings
Arrays of characters
– All strings end with null ('\0')
– Examples
• char string1[] = "hello";
– Null character implicitly added
– string1 has 6 elements
• char string1[] = { 'h', 'e', 'l', 'l',
'o', '\0’ };
– Subscripting is the same
String1[ 0 ] is 'h'
string1[ 2 ] is 'l'
Examples Using Arrays
Pointers
– Powerful, but difficult to master
– Simulate pass-by-reference
– Close relationship with arrays and strings
Pointer Variable Declarations and
Initialization
Pointer variables
– Contain memory addresses as values count
7
– Normally, variable contains specific value (direct reference)
– 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;
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
Pointer Operators
y yptr y
5 500000 600000 600000 5
yPtr
address of y
is value of
yptr
Pointer Operators
* (indirection/dereferencing operator)
– Returns synonym for object its pointer operand points to
– *yPtr returns y (because yPtr points to y).
*yptr = 9; // assigns 9 to y
* and & are inverses of each other
1 // Fig. 5.4: fig05_04.cpp
2 // Using the & and * operators.
3 #include <iostream>
4
5 using std::cout;
6 using std::endl;
7
8 int main()
9 {
10 int a; // a is an integer
11 int *aPtr; // aPtr is a pointer to an integer
12
13 a = 7;
14 aPtr = &a; // aPtr assigned address of a
15
16 cout << "The address of a is " << &a
17 << "\nThe value of aPtr is " << aPtr;
18
19 cout << "\n\nThe value of a is " << a
20 << "\nThe value of *aPtr is " << *aPtr; * and & are inverses
21 of each other
22 cout << "\n\nShowing that * and & are inverses of "
23 << "each other.\n&*aPtr = " << &*aPtr
24 << "\n*&aPtr = " << *&aPtr << endl;
25
26 return 0; // indicates successful termination
27
28 } // end main