Please answer all C++ related question (1-9) for positive rate. 1. Fact Pointers
ID: 3586152 • Letter: P
Question
Please answer all C++ related question (1-9) for positive rate.
1. Fact Pointers are one of the low-level-of-abstraction aspects of the C/C++ programming language. T or F? A pointer-to an object is the main memory address of the object that is pointed-to.
2. Why does it make good software engineering sense to prefer <array> and <vector> objects to pointer-based, C-style arrays (that is, the array data structure that is “built-into” C/C++)?
3. Define a pointer-to-int variable named px initialized to nullptr. Note Use NULL when nullptr is not supported.
4. (Continuing 3) Write an expression-statement to set px to the address-of the int variable named x.
5. (Continuing 3) Complete the stream insertion expression shown below to output the value-of x using the pointer-to-int variable px.
cout << "x = " << << endl;
6. (Continuing 3) Define a pointer-to-pointer-to-int variable named ppx initialized to the address-of px.
7. (Continuing 6) Complete the stream insertion expression shown below to output the value-of x using the pointer ppx.
cout << "x = " << << endl;
8. Change the code segment below to make fp a pointer-to-int IO formal parameter. Note fp is a call-by-reference parameter because it is a pointer, but fp is not a C++ reference parameter.
void F(int fp)
{
fp = fp * fp;
}
int main()
{
int ap;
...
F( ap );
...
}
9. Change the code segment below to make fp a C++ reference IO formal parameter.
void F(int fp)
{
fp = fp * fp;
}
int main()
{
int ap;
...
F( ap );
...
}
Explanation / Answer
1. True. Pointers are low level abstraction of C/C++ as we can directly access memory locations
2. As array are vectors are serial data types, which means and they are stored in series of continuous memory locations so we can access all the elements of the array using a pointer to first element of array.
3. int* px = NULL;
// initializing pointer to integer using NULL
4. int x;
px = &x; //storing address of x in px
5. cout << "x = " << *px << endl;
// we can print the integer by dereferencing
6. int** ppx = &px;
//initializing pointer to pointer to int to address of pointer to x that is px
7. cout << "x = " << **ppx << endl;
// we can print the integer by dereferencing twice
8.
// updated code to use pointer for pass by reference instead of pass by value
void F(int* fp)
{
*fp = (*fp) * (*fp);
}
int main()
{
int ap;
...
F( &ap );
...
}
9.
// using alias functionality for pass by reference instead of pointer to int
void F(int& fp)
{
fp = fp * fp;
}
int main()
{
int ap;
...
F( ap );
...
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.