【发布时间】: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 << x << endl;,我的程序将编译,它只会显示内存地址,它指向 x[0]。但是为什么cout << account << endl; 不起作用呢?我不应该也看到一个内存地址吗?具体来说,account 指向第一个数据成员——即 account.balance,对吗?在 PHP 中,我必须通过引用传递一个数组,所以数组在函数之外发生了变化,这让我更加困惑。为什么我不必在 C++ 中做,而必须对结构做呢? ... 那么为什么我不能打印出结构的内存地址呢?我什至可以打印出一个 Node* 的 head 的内存地址。
那么为什么结构类型是通过引用传递的呢? the_account 是一个结构。数组也是如此。然而,我们在没有引用 (&) 的情况下传递数组,并且数组在函数之外被更改。帐户不只是一个指向其数据成员的地址,就像一个数组......?这让我很困惑。
【问题讨论】:
-
数组不是你说的结构。它们是完全独立的类型分类。
-
cout
-
数组自然衰减为指向其第一个元素的指针。
x衰减为&x[0]。但是account不是指针,也不是会衰减为指针的数组。它是一个对象。要获取指向对象的指针,请使用地址运算符&,如&account。 -
@Fortunata,你可以让它编译,但你必须向编译器解释如何打印一个
CDAccount对象。类没有默认的打印行为。一个支持打印的类的例子是std::string。 -
它不会构建,因为您没有为
CDAccount类重载任何输出运算符 (operator<<)。也许你应该先get a few good books to read?
标签: c++ pointers memory structure