#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 character: " << sizeof(char) << " bytes "<< endl; cout << "Size of a double: " << sizeof(double) << " bytes " << endl; } /* * 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; } /* * 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; } } int main() { print_sizeof(); // print size of primitives 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; // why is this not allowed? //array1 = array2; // The pointer is passed by value // As a consequence, the array is passed by reference! change_first(array2); print_array(array2, SIZE); // A dynamically allocated array int *array3 = new int[SIZE]; print_array(array3, SIZE); // note that an int* variable can be passed to the print function // array3 can be on the left side of the equals array3 = array1; print_array(array3, SIZE); return 0; }