首页 > 解决方案 > 在函数中显示地址

问题描述

我正在做学校作业,但我不明白如何做 C 部分。

这是作业(除了 C 部分,我什么都有):

A. 创建一个名为 charIncrementValue 的函数,它接收 char 类型。在 driverfunction() 中,我们创建了一个 char 保存变量,我们将按值传递。让函数使用 ++ 操作更新参数变量,并将其显示在函数内部。驱动程序将在函数调用后再次显示它。

B. 创建另一个名为 charIncrementReference 的函数,它执行与 A 部分相同的操作,但这次它应该通过引用处理 char 变量。

C. 创建另一个名为 charIncrementValueShow 的函数并再次执行您在 A 部分中所做的相同操作,但这次对其进行编码以显示函数中的地址,而不是值。

我的麻烦是显示地址。我不知道如何在课程之外做到这一点printf(),我还没有在这门课上学习过,所以我不想越界。

我的代码:

#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;
    }

任务 4-C 只是 4-A 的复制/粘贴,因为我只是想要一个填充物,直到我明白需要什么。

标签: c++c++11

解决方案


推荐阅读