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

时间:2018-06-06 04:22:16

标签: c++ pointers memory 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.balance,对吧?在PHP中,我不得不通过引用传递数组,因此数组在函数之外更改,这让我更加困惑。为什么我不必在C ++中这样做,而它必须对结构做? ...那么为什么我不能打印出结构的内存地址呢?我甚至可以打印出头部的内存地址,即Node *。

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

2 个答案:

答案 0 :(得分:0)

您无法使用account打印cout<<,因为'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

答案 1 :(得分:0)

在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;
}