Pointers and Arrays as Parameters Self Test Exercises for Section 4.2 Suppose th
ID: 3665207 • Letter: P
Question
Pointers and Arrays as Parameters Self Test Exercises for Section 4.2 Suppose that p is a value parameter of type int What happens when a function does an assignment to p? a 9 When should a pointer parameter be a reference parameter? 10. that an array is passed as a parameter. How does this differ from the usual of a value 11. Write the prototype for a function called m intarray. The function takes two reference parameters: a pointer that will be used to point to the and a size-t data type to indicate the size of the 12. Write a function with one reference parameter that is a pointer to an inte ger. The function allocates a dynamic array of n integers, making the pointer point this array. It then fills the array with 0 through n -1. 13. Why do average and compare on page 175 use the keyword const with the data array, but fil1 array does not? 14. Write a function that copies n elements from the front of one integer array to the front of another. One of th arrays should be a const parame- ter, and the other should be an ordinary array parameter. 15. Describe in English the behavior of the program in Figure 4.7.Explanation / Answer
Why do average and compare on page number 175 use the keyword const with the data array, but fill_array does not?
You didn't gave an image of fill_array() in your question. Anyways, const is the keyword used to instruct that the function which is receiving the array as input is not allowed to modify it. Both the functions average and compare, are not authorized to modify(write) the data array. They are supposed to read the elements from the array, and do the required operations. Therefore, to avoid accidental manipulation of the data inside the function, its a good idea to declare the data array as const in those functions. But, coming to the fill_array(), probably it should fill something into the data array, which means it should modify(write) some values into the data array. Therefore, this function is authorized to do the modification, and the array should not be declared as const when passing to that function.
The keyword const means, the variable/array is a constant and is not allowed to be modified. Therefore, the functions which are allowed only to read should be passed with constant variables ideally. Note that, it is not a mandate to do so, but may lead to accidental manipulations.