-1

I am again puzzled. Please don't ban me from asking questions, if I can get confirmation or answers to my questions I can learn more and I will appreciate it. I browsed stack overflow and there are a lot of questions similar to what I've been asking, but they are not helping me. Note: You can copy paste the code below over here https://www.tutorialspoint.com/compile_cpp_online.php and it will work. I am sure my questions are simple for an expert.

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

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

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

In our program x is an array, and basically x[0], x[1], x[2] are data members. I can do cout << x << endl; and my program will compile and it will just show me the memory address, and it is pointing to x[0]. But why won't cout << account << endl; work? Shouldn't I also see a memory address? Specifically, account is pointing at the first data member -- that being account.balance, right? In PHP I had to pass an array by reference so the array changes outside of the function, which confuses me even more. How come I don't have to do it in C++, while it has to do done to a structure? ... So why can't I print out the memory address of a structure? I can even print out the memory address of head which is a Node*.

So why is a structure type passed by reference? the_account is a structure. So is an array. Yet we pass arrays without reference (&) and the array is changed outside of the function. Isn't account just an address that points to its data members just like an array...? This is confusing to me.

Fortunata
  • 37
  • 7
  • An array is not a structure as you say it is. They're completely separate type classifications. – chris Jun 06 '18 at 04:25
  • cout << &account << endl; –  Jun 06 '18 at 04:25
  • Arrays naturally decays to pointers to their first element. `x` decays to `&x[0]`. But `account` is not a pointer, nor an array that will decay to a pointer. It's an *object*. To get a pointer to the object use the address-of operator `&` as in `&account`. – Some programmer dude Jun 06 '18 at 04:26
  • Ok. but why does my program not compile when I do cout << account << endl;? – Fortunata Jun 06 '18 at 04:27
  • account is not an array, so treat it as a variable if you wish, in the same way you print the address of a variable x as "&x", do the same with account, "&account" –  Jun 06 '18 at 04:27
  • because it can't retain the value of a whole struct, in this case account is not a pointer like an array, is the whole thing –  Jun 06 '18 at 04:28
  • 1
    @Fortunata, You can make it compile, but you have to explain to the compiler how to print a `CDAccount` object. There's no default printing behaviour for classes. An example of a class that supports printing is `std::string`. – chris Jun 06 '18 at 04:30
  • 3
    It doesn't build because you have not overloaded any output operator (`operator<<`) for the `CDAccount` class. Perhaps you should [get a few good books to read](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) first? – Some programmer dude Jun 06 '18 at 04:30
  • The answer to your title is "yes, you can print the address of a structure" (assuming that by "structure" you mean an instance, or "object" in C++ speak. – juanchopanza Jun 06 '18 at 04:35
  • Okay, I understand these answers. Blade's answer is simplest, and makes sense. @Dude, I did not know it is an object, and I did not know I would have to define an operator to print out the memory address. Makes a lot more sense now. But ... why pass arrays by reference in PHP to get them to change outside a local function? I guess in C++, it decays to an address automatically? So that's why... hmm... Whatever, thanks for the comments. – Fortunata Jun 06 '18 at 04:47
  • You don't overload the operator to print out the address, but the object. For example, `std::string` overloads it to print the string data. To print the address, you obtain a pointer and print that. You can also get your value semantics for arrays with `std::array`. The language arrays aren't going to break compatibility with C anytime soon. – chris Jun 06 '18 at 04:49

2 Answers2

0

You can not print account by using cout<< because 'cout' does not know how to print it. You must define a function that tells the cout what you want to print from that account object. In this case, you need friend fuction. You can do as follows before 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;
  }
};

I think this link is clearer for you: https://www.tutorialspoint.com/cplusplus/cpp_friend_functions.htm

TaQuangTu
  • 2,155
  • 2
  • 16
  • 30
  • Someone has down voted this answer( my answer). Is there anybody can point me to the wrong error in my answer? Thank in advance! – TaQuangTu Jun 06 '18 at 06:58
0

In C++, there are both pointers and references. Reason for this is that pointers existed first, and references were added later.

When you print an array, in many languages it would print all elements. This is what people usually want to do. In C++, cout << array prints the memory address because arrays are handled as pointers. array decays to the pointer to the first element, &array[0].

When you pass an object (actually a reference) to cout, there is a compile error because compiler does not know what you want to print. It does not automatically convert the object to a memory address, because most people don't want to print that.

You can print the memory address of an object with cout << &account. To make cout << account work, you need to implement << operator for the class:

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

    // Or print something else
    // return out << "Account balance: " << account.balance;
}
VLL
  • 9,634
  • 1
  • 29
  • 54