我对使用 C++ 进行编码相对较新,并且被要求创建一个处理指针和地址的程序。我陷入困境的部分是使用两个内存地址之间的差异来更改分子的值(我们正在处理分数)。
这是给我的任务:http ://cdn.frugalfinders.com/assignment.pdf
我被困在问题 2 上。这是我到目前为止的 C++ 代码:
#include <iostream>
#include <fstream>
#include <cstring>
#include "fraction.h"
using namespace std;
int main()
{
int i1;
fraction f1(2,5);
int i2;
fraction farray[5];
float x1;
double x2;
char c1;
int A[6];
int *ip1;
float *fp1;
fraction *ffp1;
double *dp1;
// Print the addresses of the variables
cout << endl;
cout << "i1 at: " << &i1 << endl;
cout << "f1 at: " << &f1 << endl;
cout << "i2 at: " << &i2 << endl;
cout << "farray at: " << &farray << endl;
cout << "x1 at: " << &x1 << endl;
cout << "x2 at: " << &x2 << endl;
cout << "c1 at: " << &c1 << endl;
cout << "A at: " << &A << endl;
cout << "ip1 at: " << &ip1 << endl;
cout << "fp1 at: " << &fp1 << endl;
cout << "ffp1 at: " << &ffp1 << endl;
cout << "dp1 at: " << &dp1 << endl;
// Print the values of the variables
cout << endl;
cout << "f1 is: " << f1 << endl;
cout << "ip1 is: " << ip1 << endl;
cout << "fp1 is: " << fp1 << endl;
cout << "ffp1 is: " << ffp1 << endl;
cout << "dp1 is: " << dp1 << endl << endl;
// Store the address of i2 in ip1
ip1 = &i2;
// Change the numerator of f1 to 23
cout << f1 << endl;
i1 = (&i2) - 4;
*i1 = 23;
cout << f1 << endl;
return 0;
}
我只是不知道如何得到这个部分
// Change the numerator of f1 to 23
去工作。任何帮助是极大的赞赏!