Presentation is loading. Please wait.

Presentation is loading. Please wait.

Array and Pointers An Introduction Unit - 02. Unit Introduction This unit covers the usage of pointers and arrays in C++

Similar presentations


Presentation on theme: "Array and Pointers An Introduction Unit - 02. Unit Introduction This unit covers the usage of pointers and arrays in C++"— Presentation transcript:

1 Array and Pointers An Introduction Unit - 02

2 Unit Introduction This unit covers the usage of pointers and arrays in C++

3 Unit Objectives After covering this unit you will understand… Arrays in C++ Arrays in C++ Pointers in C++ Pointers in C++ Using pointers Using pointers Pointer Arithmetic Pointer Arithmetic Pointer constants, variables and strings Pointer constants, variables and strings Function Pointers Function Pointers

4 Array and Pointer An Array is a collection of contiguous memory blocks of same data type An Array is a collection of contiguous memory blocks of same data type An Array has a name, which is the address of the first memory block An Array has a name, which is the address of the first memory block A Pointer is a variable, which holds the address of another variable A Pointer is a variable, which holds the address of another variable

5 Arrays Array index starts at zero (0) Array index starts at zero (0) int array[10]; // reserves space for 10 int types in memory from 0 to 9 Accessing elements from an array Accessing elements from an array int y = array[0]; // Here, first element is retrieved Storing values in an array Storing values in an array array[4] = 5; // Here the 5th element is given value 5 array[4] = 5; // Here the 5th element is given value 5 Arrays are extremely fast since they are indexed Arrays are extremely fast since they are indexed An Array size cannot be changed at runtime An Array size cannot be changed at runtime

6 Example: Arrays // This program stores numbers from 0 to 9 in an array #include #include void main() { int array[10]; int array[10]; for (int j=0; j<=9; j++) for (int j=0; j<=9; j++) { array[j] = j; array[j] = j; }}

7 Pointers Pointers point to a memory space Pointers point to a memory space Pointers are denoted by ‘*’ e.g. Pointers are denoted by ‘*’ e.g. int a = 47; int a = 47; int* pointerToA = &a;

8 Example: Pointers #include #include void function(int* pointer) { cout << "p = " << *pointer << endl; // prints 4 cout << "p = " << *pointer << endl; // prints 4 *pointer = 6; *pointer = 6; cout << "p = " << *pointer << endl; // prints 6 cout << "p = " << *pointer << endl; // prints 6} void main() { int x = 4; int x = 4; function(&x);// pass the address to the function function(&x);// pass the address to the function cout << "x = " << x << endl; //prints 6 cout << "x = " << x << endl; //prints 6}

9 Pointer Arithmetic Pointer arithmetic is a special type of arithmetic Pointer arithmetic is a special type of arithmetic int* p; p++; // adds 2-bytes to the pointer address, since type // is int // is int double* pd; pd++; // adds 4-bytes to the pointer address,since type is // double Similar case for classes and structs Similar case for classes and structs The compiler knows how many bytes to go forward The compiler knows how many bytes to go forward

10 Allowed Pointer Arithmetic Following are the allowed pointer operations: Following are the allowed pointer operations: Subtraction of pointers results in the number of elements between the pointers Subtraction of pointers results in the number of elements between the pointers Addition of integral values in pointers Addition of integral values in pointers Add or subtract pointers with an integral value Add or subtract pointers with an integral value Adding two pointers is not allowed Adding two pointers is not allowed

11 Example: Pointer Arithmetic #include #include void main() { int* p1; // simple way of defining pointers int* p1; // simple way of defining pointers int* p2; int* p2; int* p3; int* p3; int x; int x; p2 = &x; p2 = &x; p1 = p2; p1 = p2; p2 = p2 + 5; p2 = p2 + 5; cout << (p2 - p1);// prints the number of elements // between the two pointers, which is 5 cout << (p2 - p1);// prints the number of elements // between the two pointers, which is 5 // p3 = p2 + p1; is illegal operation // p3 = p2 + p1; is illegal operation}

12 Constants and Variables Pointers are variables by default Pointers are variables by default Constant Pointers can be declared by using the const keyword Constant Pointers can be declared by using the const keyword int intVal = 10; int intVal = 10; int* const intPointer = &intVal; /* intPointer is a pointer, which is const, that points to /* intPointer is a pointer, which is const, that points to an int */ an int */ In the above example you can change the value in intVal but not the address In the above example you can change the value in intVal but not the address *intPointer = 20; /* allowed because the pointer (i.e. the address) is constant, while the value inside the pointer can be variable */ /* allowed because the pointer (i.e. the address) is constant, while the value inside the pointer can be variable */

13 Example: Constants & Variables #include #include void main() { int b = 25; int b = 25; /* (shown below) an int pointer that is stored at a /* (shown below) an int pointer that is stored at a constant memory address */ constant memory address */ int* const a = &b; // ‘a’ holds the address of ‘b’ // which has a value of 25 int* const a = &b; // ‘a’ holds the address of ‘b’ // which has a value of 25 b = 35; // ‘a’ still points to ‘b’ which now has 35 b = 35; // ‘a’ still points to ‘b’ which now has 35 *a = 45; // allowed - since the value can change // inside the address that the pointer points // to *a = 45; // allowed - since the value can change // inside the address that the pointer points // to}

14 Array as arguments Arrays are passed by reference in functions Arrays are passed by reference in functions Arrays can’t be passed by value Arrays can’t be passed by value

15 Example: Array as arguments #include #include function(int* array) { // you can use int array[] as arg // you can use int array[] as arg // do something with the array // do something with the array} void main() { int array[3] = { 1, 2, 3 }; int array[3] = { 1, 2, 3 }; function(array); // any changes made in array inside the function(array); // any changes made in array inside the } // function are reflected in the array

16 Pointers and String in C++ String declaration in C++ String declaration in C++ As an array // char charArray[10]; As an array // char charArray[10]; As a char* // char* charString; As a char* // char* charString; As a string // string stringTypeArray; As a string // string stringTypeArray; Array of pointers declaration e.g. Array of pointers declaration e.g. char* arrayOfPointers[10]; // array of 10 pointers for (int j=0; j<10; j++) { arrayOfPointers[j] = strcat(“String ”, j); arrayOfPointers[j] = strcat(“String ”, j);}

17 Function Pointers Pointer that can hold function addresses is called Function Pointer Pointer that can hold function addresses is called Function Pointer Steps required are Steps required are Define a function pointer Define a function pointer Initialise the pointer with function address Initialise the pointer with function address Call the function by using pointer Call the function by using pointer While defining a function pointer, parenthesis are required otherwise it becomes function declaration While defining a function pointer, parenthesis are required otherwise it becomes function declaration

18 Example: Function Pointer void func() { cout << "func() called..." << endl; cout << "func() called..." << endl;} int main() { void (*fp)(); // define a function pointer void (*fp)(); // define a function pointer fp = func; // Initialise it, assign function address fp = func; // Initialise it, assign function address (*fp)(); // call function using function pointer (*fp)(); // call function using function pointer}

19 Complex Function Pointers A function pointer can hold address of another pointer to a function, e.g. A function pointer can hold address of another pointer to a function, e.g. double ((*fp1)(int))(float)); double ((*fp1)(int))(float)); fp1 is a pointer to a function that takes an integer argument and returns a pointer to another function that takes a float and returns double fp1 is a pointer to a function that takes an integer argument and returns a pointer to another function that takes a float and returns double

20 Array of Function Pointers Like array of simple pointers, there can also be array of function pointers Like array of simple pointers, there can also be array of function pointers Useful for table-driven code implementation Useful for table-driven code implementation Instead of using if-else or case statements use a state variable Instead of using if-else or case statements use a state variable

21 Example: Array of Function Pointers // define functions a(), b() and c() void func1() { cout << ”func1() called..." << endl; cout << ”func1() called..." << endl;} void func2() { cout << ”func2() called..." << endl; cout << ”func2() called..." << endl;} void func3() { cout << ”func3() called..." << endl; cout << ”func3() called..." << endl;} // func_table is an array of function pointers to functions that accept nothing and return void // initialise array with function addresses void (*func_table[])() = {func1, func2, func3 };

22 Example: Array of Function Pointers (contd.) void main() { cout << ”Enter a number (1-3) to call a function: “; int n; cin >> n; if (n 3) { cout << “Invalid number,should be 1-3” << endl; } // call function using function pointer (*func_table[n-1]();// n is a state variable }

23 Unit Summary In this unit you have covered … Arrays Arrays Pointers Pointers Uses of Pointers Uses of Pointers


Download ppt "Array and Pointers An Introduction Unit - 02. Unit Introduction This unit covers the usage of pointers and arrays in C++"

Similar presentations


Ads by Google