c++ - 在函数中显示地址
问题描述
我正在做学校作业,但我不明白如何做 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 的复制/粘贴,因为我只是想要一个填充物,直到我明白需要什么。
解决方案
推荐阅读
- php - 分组和分页不能一起工作 laravel
- swift - Swift中多个对象的单个阴影?
- azure-cosmosdb - Azure Cosmos DB MongoDB api 日期时间格式
- r - 如何使闪亮的主面板占据屏幕的整个宽度
- java - 用Java中的距离和坐标计算位置
- java - 接口作为属性?
- vue.js - 带有验证器的 vuetify 中的日期选择器
- svn - 使用 Sublime Text 作为 SVN 并排比较工具
- java - Firebase RTDB 在 Android 应用中没有响应
- javascript - Highcharts 甘特图或任何图表 x 轴滚动位置位于加载图表的开始/左侧