C++可以输出一个结构的内存地址吗
C++ can we output the memory address of a structure?
我再次感到困惑。请不要禁止我提问,如果我能得到问题的确认或答案,我可以了解更多信息,我将不胜感激。我浏览了堆栈溢出,有很多问题与我一直在问的问题类似,但它们对我没有帮助。注意:你可以将下面的代码复制粘贴到这里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
- 预处理器:插入结构名称中的前一个行号
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 我不明白为什么我声明一个空的内部结构并将其传递给构造函数
- 静态结构和一个定义规则
- 为什么将一个结构的引用设置为等于另一个结构只会更改一个数据成员?
- C++ - 如何在结构向量中找到结构体一个成员的最大值?
- 如何在 C++11 中查找和更新向量中的一个嵌套结构
- 在学习数据结构之前对STL有一个了解是好的吗?
- 给定一个C++嵌套的私有结构类型,是否有从文件范围静态函数访问它的策略
- 从另一个 cpp 文件更改结构内、映射键内的变量
- 如何将一个结构的字符数组复制到结构的另一个字符数组中?
- 使用完数据结构后清空数据结构是一个好习惯吗?
- 如何制作一个地图,其中的值是C++中的结构数组
- 如何将数据从一个结构链接到另一个结构
- 是否可以使用智能指针成员设置具有另一个结构的结构?
- error dllimport 函数的定义不允许在一个特定的联合中,而其他类、结构和联合将按预期导出
- std::transform 将一个结构数组复制到另一个结构数组
- C++:如何做一个链表,结构作为参数传递?
- 输入 ctrl + x 后如何再次 cin (cin<<x) 循环(cin 一个结构)
- 两个附带的类层次结构-一个好的设计模式