Pointers and Arrays as Parameters Self Test Exercises for Section 4.2 Suppose th
ID: 3665205 • 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
Q8: int *p;
int i = 5;
p = &i;
printf(“%d”, *p);
has the same effect as printf(“%d”, i);
The * has an effect to derefence the &
hence *p = * & i = i
= 5
Q9: The pointer parameter becomes a reference parameter at the following situations:
Pass by Value does not change the value of the variable at the calling function
Any changes made in the called function does not reflect back in the calling function
But
Pass by Reference reflects the change
example:
void functionDisplay(int a) { // a = 5
printf(“%d”, a++); // 5 is printed, a gets changed to 6
printf(“%d”, a); // 6 is printed
}
void main() {
int b = 5;
printf(“ Before the call %d”, b); // prints 5
functionDisplay(b); // inside the function value is changed
printf(“ After the call %d”, b); // but is not reflected here – still prints 5
}
But when called by reference value will be changed in the calling function as well
void functionDisplay(int *a) { // a = 5
printf(“%d”, a++); // 5 is printed, a gets changed to 6
printf(“%d”, a); // 6 is printed
}
void main() {
int b = 5;
printf(“ Before the call %d”, b); // prints 5
functionDisplay(&b); // inside the function value is changed
printf(“ After the call %d”, b); // and is also reflected here – prints 6
}
Q10: Example for passing an entire array as a parameter to a function is shown below:
int findSum(int array1[], int length) { // the whole array is passed in as a parameter
int j, sum;
for (j = 0; j< length; j++)
sum = sum + array1[j];
return sum; // sum = 55
} // end function
void main() {
int total ;
int array2[10] = { 4,5,1,2,3,6,7,9,7,10 };
total = findSum(array2, 10); // 55 is returned
}
Q11: Prototype for a function make_intarray
make_intarray(int *array[], int size_t) ;
Q12: function with 1 reference parameter which is a pointer to an integer.
function12(int *ptr1) {
int dynaArray[n];
ptr1 = &dynaArray;
for (int i=0; i<= n-1; i++)
dynaArray[i] = i;
} // end function12