C++可以输出一个结构的内存地址吗

C++ can we output the memory address of a structure?

本文关键字:结构 一个 内存 地址 输出 C++      更新时间:2023-10-16

我再次感到困惑。请不要禁止我提问,如果我能得到问题的确认或答案,我可以了解更多信息,我将不胜感激。我浏览了堆栈溢出,有很多问题与我一直在问的问题类似,但它们对我没有帮助。注意:你可以将下面的代码复制粘贴到这里https://www.tutorialspoint.com/compile_cpp_online.php它会起作用的。我相信我的问题对于专家来说很简单。

//--------------------------------------------------

#include <cstdlib>
#include <iostream>
using namespace std;
struct Node
{
int data;
Node *link;
};
struct CDAccount
{
double balance;
double interest;
int term;
};
void get_data(CDAccount& the_account);
void head_insert(Node* &head, int the_number);
void changeArray(int array[]);
Node* search(Node* head, int target); // return type is an address in 
//memory, where the address points to some Node.
int main(int argc, char *argv[]){
//Array demonstration.
int x[10] = {1,2,3,4,5,6,7,8,9,10};
for (int i=0; i<10; i++){
cout << x[i] << endl;
cout << x + i << endl;
}
cout <<endl << endl;
changeArray(x);
for (int i=0; i<10; i++){
cout << x[i] << endl;
cout << x + i << endl;
}
cout<< endl << endl;
Node* head = new Node; // head points to some Node.
cout << head << " pointing to some new Node containing 5 and new Node (see next lines)"<< endl << endl;
//cout << &head->data << endl; Same address as above.
(*head).data = 5; // head data content is 5.
(*head).link = new Node; // head pointer content points to 2nd Node.
cout << head->data << endl;
cout << head->link << endl << endl;
//(*((*head).link)).data = 20;
head->link->data = 20; // same as line before.
head->link->link = new Node;
cout << head->link->data << endl;
cout << head->link->link << endl << endl;

head->link->link->data = 25;
head->link->link->link = NULL;
cout << head->link->link->data << endl;
cout << head->link->link->link << endl << endl;
Node* found = search(head, 20);
cout<<"Target is at this address: " << found<<endl<<endl;
if(found != NULL){
cout<<(*found).data<<endl;
cout<<(*found).link<<endl; 
}

CDAccount account;
account.balance = 100;
cout << account.balance << endl;
// SAME...
cout << &account <<endl; 
cout << &account.balance<< endl;
// SAME...
cout << x << endl;
cout << &x[0] << endl;
//cout << account << endl; //WON'T WORK, WHY?
get_data(account);
cout << account.balance << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
void head_insert(Node* &head, int the_number)
{
Node* temp_ptr;
temp_ptr = new Node;
temp_ptr->data = the_number;
temp_ptr->link = head;
head = temp_ptr;
}
void get_data(CDAccount& the_account){
cout << "Inside function : " << &the_account << endl;
the_account.balance = 100000;
the_account.interest = 0.02;
the_account.term = 12;
}
void changeArray(int array[]){
array[2] = 7;
array[3] = 101;
}
Node* search(Node* head, int target)
{
Node* here = head;
if (here == NULL)
{
return NULL;
}
else
{
while (here->data != target && here->link != NULL)
here = here->link;
if (here->data == target)
return here;
else
return NULL;
}
}

//--------------------------------------------------

在我们的程序中,x是一个数组,基本上x[0]、x[1]、x[2]是数据成员。我可以做cout << x << endl;,我的程序会编译,它只会显示内存地址,它指向x[0]。但为什么cout << account << endl;不起作用呢?我不应该也看到一个内存地址吗?具体来说,account指向第一个数据成员,即account.balance,对吗?在PHP中,我必须通过引用传递数组,这样数组在函数之外就会发生变化,这让我更加困惑。为什么我不必在C++中做它,而它必须对结构做它。。。那么,为什么我不能打印出一个结构的内存地址呢?我甚至可以打印出头的内存地址,它是一个节点*。

那么,为什么结构类型是通过引用传递的呢?帐户是一个结构。数组也是如此。然而,我们传递的数组没有引用(&),并且数组在函数之外发生了更改。账户不就是一个指向其数据成员的地址,就像数组一样吗。。。?这让我很困惑。

在C++中,既有指针也有引用。原因是指针首先存在,而引用是后来添加的。

打印数组时,在许多语言中,它会打印所有元素。这是人们通常想要做的。在C++中,cout << array打印内存地址,因为数组是作为指针处理的。array衰减到指向第一个元素&array[0]的指针。

当您将一个对象(实际上是一个引用)传递给cout时,会出现编译错误,因为编译器不知道您要打印什么。它不会自动将对象转换为内存地址,因为大多数人不想打印它。

您可以使用cout << &account打印对象的内存地址。为了使cout << account工作,您需要为类实现<<运算符:

ostream& operator<<(ostream& out, const CDAccount& account) {
// Print memory address
return out << &account;
// Or print something else
// return out << "Account balance: " << account.balance;
}

不能使用cout<<打印account,因为"cout"不知道如何打印。您必须定义一个函数,"告诉"cout您想从该account对象打印什么。在这种情况下,您需要friend fuction。在cout<<accout之前,您可以执行以下操作:

class CDAccount
{
public: //or private or protected
double balance;
double interest;
int term;
friend ostream& operater<<(ostream&out, const CDAccount& account)
{
//print infor of account object
return out<<account.balance<<" "<<account.interest<<" "account.term;
}
};

我认为这个链接对你来说更清晰:https://www.tutorialspoint.com/cplusplus/cpp_friend_functions.htm