Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

heres the question : In C++ •Implement this function: int* foo (int* paramPtr1,

ID: 3823098 • Letter: H

Question

heres the question : In C++

•Implement this function:

int* foo (int* paramPtr1, int*& paramPtr2, const int* paramPtr3);

Since they are pointers, set them all to null in the function.

•In the main function, create 3 integer pointers and allocate memory with an arbitrary dereferenced values. Then call ‘foo’ and pass three pointers to this function.

•Upon returning from ‘foo’, explain the effects of setting the parameter pointers in ‘foo’ on these 3 arguments

heres my code

#include <iostream>

using namespace std;

//int* foo (int* paramPtr1, int*& paramPtr2, const int* paramPtr3);

int* foo (int* paramPtr1, int*& paramPtr2, const int* paramPtr3);

int main()

{

int x= 1;

int j= 2;

int *ptr1= new int;

*ptr1 = x; // explict deferencing , so when printed out its printing the value its pointing to instead of the memory location.

  

int *ptr2= new int;

*ptr2 = j;

  

int *ptr3= new int;

*ptr3 = 3;

   foo(ptr1, ptr2, ptr3); // upon calling function "foo" parameter ptr1 is passed in by value so when first printed it is showing the value when pointed to in main (1) but when derefrecened and printed out again it is then dispalying 0 or Null. When para

  

cout<< *ptr1<<" " << *ptr2<< " " << *ptr3<< endl;

  

  

return 0;

}

int* foo(int* paramPtr1, int*& paramPtr2, const int* paramPtr3)

{

cout << "paramPtr1 before set to Null :" << " " << *paramPtr1 <<endl;

*paramPtr1 = NULL;

*paramPtr1 += *paramPtr1;

cout << "paramPtr1 after dereferncing :" << " "<< *paramPtr1 <<endl;

  

cout <<"paramPtr2 before set to Null :"<< " "<<*paramPtr2 <<endl; // pointer

   *paramPtr2 = NULL; //

*paramPtr2 += *paramPtr2;

cout << "paramPtr2 after dereferncing :" << " "<< *paramPtr2<<endl;

  

cout << "paramPtr3 before set to Null :" << " " << *paramPtr3 <<endl;

//*paramPtr3 = NULL;//read only variable error message if uncommented.

//*paramPtr3 IS NOT Assignable, so you cannot assign it to Null - the paramter is pointed to ptr1 which value set in Main (3) is displayed and cannot be changed by assignment. This will always be the value of 3.

  

  

return NULL;

}

Dont know what im doing wrong to not change the value of ptr1?

Explanation / Answer

HI, To set null you have to do: paramPtr1 = NULL; NOT *paramPtr1 = NULL;

#include <iostream>
using namespace std;

//int* foo (int* paramPtr1, int*& paramPtr2, const int* paramPtr3);
int* foo (int* paramPtr1, int*& paramPtr2, const int* paramPtr3);
int main()
{

int x= 1;
int j= 2;
int *ptr1= new int;
*ptr1 = x; // explict deferencing , so when printed out its printing the value its pointing to instead of the memory location.
  
int *ptr2= new int;
*ptr2 = j;
  
int *ptr3= new int;
*ptr3 = 3;

foo(ptr1, ptr2, ptr3); // upon calling function "foo" parameter ptr1 is passed in by value so when first printed it is showing the value when pointed to in main (1) but when derefrecened and printed out again it is then dispalying 0 or Null. When para
  
cout<< *ptr1<<" " << *ptr2<< " " << *ptr3<< endl;
  
  
return 0;
}

int* foo(int* paramPtr1, int*& paramPtr2, const int* paramPtr3)
{
cout << "paramPtr1 before set to Null :" << " " << *paramPtr1 <<endl;
paramPtr1 = NULL;
//*paramPtr1 += *paramPtr1;
cout << "paramPtr1 after dereferncing :" << " "<< *paramPtr1 <<endl;
  
cout <<"paramPtr2 before set to Null :"<< " "<<*paramPtr2 <<endl; // pointer
paramPtr2 = NULL; //
// *paramPtr2 += *paramPtr2;
cout << "paramPtr2 after dereferncing :" << " "<< *paramPtr2<<endl;
  
cout << "paramPtr3 before set to Null :" << " " << *paramPtr3 <<endl;
//*paramPtr3 = NULL;//read only variable error message if uncommented.
//*paramPtr3 IS NOT Assignable, so you cannot assign it to Null - the paramter is pointed to ptr1 which value set in Main (3) is displayed and cannot be changed by assignment. This will always be the value of 3.
  
  
return NULL;
}

Please let me know in case of any issue