#include using namespace std; /* * Since the print_array function is called by many others, * I include its function prototype at the top of the file. */ void print_array(int array[], int length); /* * Prints the size in bytes of various primitive types */ void print_sizeof() { cout << "Size of an integer: " << sizeof(int) << " bytes "<< endl; cout << "Size of a long integer: " << sizeof(long int) << " bytes " << endl; cout << "Size of an unsigned integer: " << sizeof(unsigned int) << " bytes " << endl; cout << "Size of a character: " << sizeof(char) << " bytes "<< endl; cout << "Size of a double: " << sizeof(double) << " bytes " << endl; } /* * Prints an array */ void print_array(int array[], int length) { cout << endl; cout << "Printing array: " << endl; for(int i = 0; i < length; ++i) { cout << array[i] << endl; } } /* * Examples of using pointer arithmetic on an array */ void pointer_arithmetic() { int SIZE = 5; int array[SIZE]; for(int i = 0; i < SIZE; ++i) { array[i] = i; } print_array(array, SIZE); cout << "address array[0]= " << array << endl; cout << "address array[1]= " << array+1 << endl; cout << "address array[2]= " << array+2 << endl; cout << endl; cout << "array[3] = " << array[3] << endl; // value of 3rd element cout << "*(array+3) = " << *(array+3) << endl; // value of 3rd element for(int* ptr = array; ptr < array + SIZE; ++ptr) { *ptr = 2*(*ptr); // multiply each element by 2 } cout << "Multipled each element by 2..." << endl; print_array(array, SIZE); } /* * Passes the pointer by value but the net effect is that the * entire array itself is passed by reference */ void change_first(int array[]) { array[0] = -100; } /* * Passes the pointer by value. As a result, we can change the contents * of the input argument but we cannot change the input argument itself. */ void has_no_effect(int array[]) { cout << "\tAddress of formal parameter: " << array << endl; // Create a new array int new_array[] = {3, 3, 3, 3, 3}; // Change formal parameter to point to new array array = new_array; cout << "\tAddress of formal parameter: " << array << endl; } /* * Different ways of initializing an array */ void initializing_arrays() { int SIZE = 5; // An uninitialized array int array1[SIZE]; cout << "Size of the array: " << sizeof(array1) << " bytes " << endl; cout << "Length of array: " << sizeof(array1)/sizeof(array1[0]) << endl; print_array(array1, SIZE); cout << "Accessing beyond bounds! " << array1[SIZE] << endl; // Create second array using initialization list int array2[] = {0, 0, 0, 0, 0}; print_array(array2, SIZE); cout << "Accessing beyond bounds! " << array2[SIZE] << endl; } /* * */ void dynamically_allocated_array() { int SIZE = 5; // A dynamically allocated array int *array = new int[SIZE]; print_array(array, SIZE); // note that an int* variable can be passed to the print function int array1[SIZE]; // array can be on the left side of the equals array = array1; print_array(array, SIZE); } int main() { print_sizeof(); initializing_arrays(); pointer_arithmetic(); // Pass-by-value int my_array[] = {0, 0, 0, 0, 0}; cout << "Address of input argument: " << my_array << endl; has_no_effect(my_array); cout << "Address of input argument: " << my_array << endl; // Pass-by-reference print_array(my_array, 5); change_first(my_array); print_array(my_array, 5); dynamically_allocated_array(); return 0; }