Suppose that p is a value parameter of type int*. What happens when a function d
ID: 3665189 • Letter: S
Question
Suppose that p is a value parameter of type int*. What happens when a function does an assignment to *p? When should a pointer parameter be a reference parameter? Suppose that an array is passed as a parameter. How does this differ from the usual use of a value parameter? Write the prototype for a function called make_intarray. The function takes two reference parameters: a pointer that will be used to point to the array, and a size_t data type to indicate the size o0f the array. Write a Function with one reference parameter that is a pointer to an integer. The function allocates a dynamic array of n integers, making the pointer point to this new array. If then fills the array with 0 through n - 1. Why do average and compare on page175 use the keyboard const with the data array, but fill_array does not? Write a function that copies n elements from the front of one integer array to the front of another. One of the arrays should be a const parameter, and the other should be an ordinary array parameter. Describe in English the behaviour of the program in Figure 4.7.Explanation / Answer
9.) A const function formal parameter cannot be modified inside the function. Use const whenever possible as it protects you from inadvertently modifying the parameter and protects you against many programming errors.
A const function parameter can receive both const and non-const argument. On the other hand, a non-const function reference/pointer parameter can only receive non-const argument. For example,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
/* Test Function const and non-const parameter (FuncationConstParameter.cpp) */ #include <iostream> using namespace std; int squareConst(const int); int squareNonConst(int); int squareConstRef(const int &); int squareNonConstRef(int &); int main() { int number = 8; const int constNumber = 9; cout << squareConst(number) << endl; cout << squareConst(constNumber) << endl; cout << squareNonConst(number) << endl; cout << squareNonConst(constNumber) << endl; cout << squareConstRef(number) << endl; cout << squareConstRef(constNumber) << endl; cout << squareNonConstRef(number) << endl; // cout << squareNonConstRef(constNumber) << endl; // error: invalid initialization of reference of // type 'int&' from expression of type 'const int' } int squareConst(const int number) { // number *= number; // error: assignment of read-only parameter return number * number; } int squareNonConst(int number) { // non-const parameter number *= number; return number; } int squareConstRef(const int & number) { // const reference return number * number; } int squareNonConstRef(int & number) { // non-const reference return number * number; } Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.