mercredi 23 juin 2021

Display Address in Function [duplicate]

I'm working on a school assignment, but I don't understand how to do part C.

Here's the assignment (I have everything but part C):

"A. Create a function called charIncrementValuethat receives char types. In the driverfunction() we created a char holding variable that we’ll pass by value. Have the function update the parameter variable with a ++operation, and display it inside the function. The driver will then display it again after the function call.

B. Create another function called charIncrementReference that does the same things you did in part A, but this time it should process the char variable by reference.

C. Create another function called charIncrementValueShow and do the same things again that you did in part A, but this time code it to display the address in the function, rather than the value."

My trouble is displaying the address. I don't know how to do that outside of printf, which I haven't learned in this class yet, so I don't want to over step the boundaries.

My Code:

#include <cassert>
#include <iosfwd>
#include <iostream>
#include <iomanip>
#include <limits>  
#include <cmath> 

using std::cout;
using std::cin;
using std::endl;

        //Task 4 Prototypes
    
    void charIncrementValue(char charVar);
    void charIncrementReference(char &charVar);
    void charIncrementValueShow(char charVar);
    
    void
    passBy()
    {
      // 4-A
      char holdVar = 'g';
      charIncrementValue(holdVar);
      cout << "holdVar = " << holdVar << endl;
    
      cout << "end of part 4-A" << endl;
      cin.get();
      // 4-B
      char holdVar2 = 'g';
      charIncrementReference(holdVar2);
      cout << "holdVar2 = " << holdVar2 << endl;
    
      cout << "end of part 4-B" << endl;
      cin.get();
      // 4-C
      char holdVar3 = 'g';
      charIncrementValueShow(holdVar3);
      cout << "holdVar3 = " << holdVar3 << endl;
    
      cout << "end of part 4-C" << endl;
      cout << "end of pass passBy" << endl;
      cin.get();
    }
    
    //Task 4-A function to increment a value passed by value
    void charIncrementValue(char charVar)
    {
        ++charVar;
        cout << "charVar = " << charVar << endl;
    }
    
    //Task 4-B function to increment a value passed by reference
    void charIncrementReference(char &charVar)
    {
        ++charVar;
        cout << "charVar = " << charVar << endl;
    }
    
    //Task 4-C function to increment the address of a value
    void charIncrementValueShow(char charVar)
    {
        ++charVar;
        cout << "charVar = " << charVar << endl;
    }

Task 4-C is the just a copy and paste of 4-A since I just wanted a filler until I understood what was needed.

Aucun commentaire:

Enregistrer un commentaire