【问题标题】:C++ can we output the memory address of a structure?C++我们可以输出结构的内存地址吗?
【发布时间】:2018-06-06 04:22:15
【问题描述】:

我又疑惑了。请不要禁止我提问,如果我能得到确认或回答我的问题,我可以了解更多信息,我将不胜感激。我浏览了堆栈溢出,有很多类似于我一直在问的问题,但它们对我没有帮助。注意:您可以将下面的代码复制粘贴到此处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 &lt;&lt; x &lt;&lt; endl;,我的程序将编译,它只会显示内存地址,它指向 x[0]。但是为什么cout &lt;&lt; account &lt;&lt; endl; 不起作用呢?我不应该也看到一个内存地址吗?具体来说,account 指向第一个数据成员——即 account.balance,对吗?在 PHP 中,我必须通过引用传递一个数组,所以数组在函数之外发生了变化,这让我更加困惑。为什么我不必在 C++ 中做,而必须对结构做呢? ... 那么为什么我不能打印出结构的内存地址呢?我什至可以打印出一个 Node* 的 head 的内存地址。

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

【问题讨论】:

  • 数组不是你说的结构。它们是完全独立的类型分类。
  • cout
  • 数组自然衰减为指向其第一个元素的指针。 x 衰减为 &amp;x[0]。但是account 不是指针,也不是会衰减为指针的数组。它是一个对象。要获取指向对象的指针,请使用地址运算符&amp;,如&amp;account
  • @Fortunata,你可以让它编译,但你必须向编译器解释如何打印一个CDAccount对象。类没有默认的打印行为。一个支持打印的类的例子是std::string
  • 它不会构建,因为您没有为 CDAccount 类重载任何输出运算符 (operator&lt;&lt;)。也许你应该先get a few good books to read

标签: c++ pointers memory structure


【解决方案1】:

在 C++ 中,既有指针又有引用。原因是指针先存在,引用是后加的。

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

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

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

ostream& operator<<(ostream& out, const CDAccount& account) {
    // Print memory address
    return out << &account;

    // Or print something else
    // return out << "Account balance: " << account.balance;
}

【讨论】:

  • 谢谢,我必须做一些进一步的研究才能完全理解。
【解决方案2】:

您不能使用cout&lt;&lt; 打印account,因为'cout' 不知道如何打印它。您必须定义一个函数,告诉cout 您想从该account 对象打印什么。在这种情况下,您需要friend fuction。在cout&lt;&lt;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

【讨论】:

  • 有人否决了这个答案(我的答案)。有没有人可以指出我的答案中的错误错误?提前致谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-02
  • 1970-01-01
  • 2012-05-09
  • 1970-01-01
  • 2013-01-02
  • 2012-12-25
相关资源
最近更新 更多