// 24T3 COMP1511 Week 7 Lecture 1 // A demonstration to show how arrays are stored in memory #include #define MAX_LEN 4 void print_array(int *nums, int length); void print_array2(int nums[], int length); int main(void) { int array[MAX_LEN] = {9, 2, 3, 7}; // array is the name of a block of memory // array is not a pointer variable and we can't change where it points to // but the name of the array decays to pointer to the first element printf("array address: %p\n", array); int i = 0; while (i < MAX_LEN) { printf("Address of array[%d] = %p\n", i, &array[i]); i++; } print_array(array, MAX_LEN); print_array2(array, MAX_LEN); return 0; } // Can pass in array to function with pointer prototype // Can use array indexes with pointers void print_array(int *nums, int length) { printf("address of array in print_array: %p\n", nums); for (int i = 0; i < length; i++) { printf("%d\n", nums[i]); } } // Can pass in array to function with pointer prototype // Can use array indexes with pointers void print_array2(int nums[], int length) { printf("address of array in print_array: %p\n", nums); for (int i = 0; i < length; i++) { printf("%d\n", nums[i]); } }