Unit - 02 Array and Pointers An Introduction Unit Introduction This unit covers the usage of pointers and arrays in C++ Unit Objectives After covering this unit you will understand… Arrays in C++ Pointers in C++ Using pointers Pointer Arithmetic Pointer constants, variables and strings Function Pointers Array and Pointer 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 A Pointer is a variable, which holds the address of another variable Arrays 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 int y = array[0]; // Here, first element is retrieved Storing values in an array array[4] = 5; // Here the 5th element is given value 5 Arrays are extremely fast since they are indexed An Array size cannot be changed at runtime Example: Arrays // This program stores numbers from 0 to 9 in an array #include <stdlib.h> void main() { int array[10]; for (int j=0; j<=9; j++) { array[j] = j; } } Pointers Pointers point to a memory space Pointers are denoted by ‘*’ e.g. int a = 47; int* pointerToA = &a; Example: Pointers #include <stdlib.h> #include <iostream.h> void function(int* pointer) { cout << "p = " << *pointer << endl; // prints 4 *pointer = 6; cout << "p = " << *pointer << endl; // prints 6 } void main() { int x = 4; function(&x); // pass the address to the function cout << "x = " << x << endl; //prints 6 } Pointer Arithmetic Pointer arithmetic is a special type of arithmetic int* p; p++; // adds 2-bytes to the pointer address, since type // is int double* pd; pd++; // adds 4-bytes to the pointer address,since type is // double Similar case for classes and structs The compiler knows how many bytes to go forward Allowed Pointer Arithmetic Following are the allowed pointer operations: Subtraction of pointers results in the number of elements between the pointers Addition of integral values in pointers Add or subtract pointers with an integral value Adding two pointers is not allowed Example: Pointer Arithmetic #include <iostream.h> void main() { int* p1; // simple way of defining pointers int* p2; int* p3; int x; p2 = &x; p1 = p2; p2 = p2 + 5; cout << (p2 - p1);// prints the number of elements // between the two pointers, which is 5 // p3 = p2 + p1; is illegal operation } Constants and Variables Pointers are variables by default Constant Pointers can be declared by using the const keyword int intVal = 10; int* const intPointer = &intVal; /* intPointer is a pointer, which is const, that points to an int */ 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 */ Example: Constants & Variables #include <iostream.h> void main() { int b = 25; /* (shown below) an int pointer that is stored at a constant memory address */ int* const a = &b; b = 35; *a = 45; } // // // // // ‘a’ holds the address of ‘b’ // which has a value of 25 ‘a’ still points to ‘b’ which now has 35 allowed - since the value can change inside the address that the pointer points to Array as arguments Arrays are passed by reference in functions Arrays can’t be passed by value Example: Array as arguments #include <stdlib.h> function(int* array) { // you can use int array[] as arg // do something with the array } void main() { int array[3] = { 1, 2, 3 }; function(array); // any changes made in array inside the } // function are reflected in the array Pointers and String in C++ String declaration in C++ As an array As a char* As a string // char charArray[10]; // char* charString; // string stringTypeArray; 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); } Function Pointers Pointer that can hold function addresses is called Function Pointer Steps required are Define a function pointer Initialise the pointer with function address Call the function by using pointer While defining a function pointer, parenthesis are required otherwise it becomes function declaration Example: Function Pointer void func() { cout << "func() called..." << endl; } int main() { void (*fp)(); fp = func; (*fp)(); } // define a function pointer // Initialise it, assign function address // call function using function pointer Complex Function Pointers A function pointer can hold address of another pointer to a function, e.g. 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 Array of Function Pointers Like array of simple pointers, there can also be array of function pointers Useful for table-driven code implementation Instead of using if-else or case statements use a state variable Example: Array of Function Pointers // define functions a(), b() and c() void func1() { cout << ”func1() called..." << endl; } void func2() { cout << ”func2() called..." << endl; } void func3() { 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 }; Example: Array of Function Pointers (contd.) void main() { cout << ”Enter a number (1-3) to call a function: “; int n; cin >> n; if (n < 1 || n > 3) { cout << “Invalid number,should be 1-3” << endl; } // call function using function pointer (*func_table[n-1](); // n is a state variable } Unit Summary In this unit you have covered … Arrays Pointers Uses of Pointers