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.