This is sort of bugging me as well.
I have code that looks like so :
1 2 3 4 5 6 7 8 9 10 11 12
|
struct debts mr_E[3] =
{
{"Ima Wolfe", 2400.0},
{"Ura Foxe", 1300.0},
{"Iby Stout", 1800.0}
};
double * pd[3];
//set pd pointers to the amount members of the structures in the arr mr_E
for (int i = 0; i < 3; i++)
{
pd[i] = &mr_E[i].amount;
}
| |
Later on in the program a call is made to this function to spit out the values:
1 2 3 4 5 6 7 8 9
|
template <class T>
void ShowArray(T * arr[], int n)
{
using namespace std;
cout << "template B\n";
for (int i = 0; i < n; i++)
cout << *arr[i] << ' ';
cout << endl;
}
| |
This works fine and dandy, but what I don't comprehend is why can't write
*pd[i] = mr_E[i].amount;
instead of the previously listed
pd[i] = &mr_E[i].amount.
I mean, I can write it.. but the compiler doesn't like it and spits out mumbo jumbo for responses past the first value. Why is this? I thought they basically meant the same thing(either that.... or I need to seriously review pointers because this is what I have believed all along.)
Version # 1: "set pointer pd[i] equal to the value stored at lcoation mr_E[i].amount"
Version # 2: "set the value of pointer pd[i] equal to mr_E[i].amount"
Also.. to make sure I still remember; & is the referencing operator and * is the dereference, correct?