Help me understand something(part 2, pointers)

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?
There's a rather simple reason to that - pd is just an array of pointers. These pointers haven't been initialized yet, so they contain garbage values. So if you did *pd[i] = mr_E[i].amount; you would assign your values to completely random places in memory that probably don't even belong to you. That version would work if you actually allocated memory for these pointers to point to with new.

PS: In C++, you don't need the struct keyword in front of a variable declaration - it's implicit.
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.)


They are VERY different.

pd[i] = &mr_E[i].amount;
This makes the pointer pd[i] point to the variable &mr_E[i].amount

*pd[i] = mr_E[i].amount;
This takes whatever pd[i] points to and assigns mr_E[i].amount to it.

That is, the former changes the pointer. The later changes what the pointer points to. Since pd[i] effectively points to "nothing" initially, doing the second will write to some random area of memory which is very bad (potentially will crash the program -- or might make it behave very strange).



Let's put this in simpler terms:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int var;   // an integer
int* ptr;  // a pointer to an integer

// we want to make it so that ptr points to var
ptr = &var;  // that's what this does

// now, "*ptr" and "var" both refer to the same variable -- the same block of memory
//  so we can do this:
*ptr = 5;
cout << var; // prints "5"

var = 10;
cout << *ptr;  // prints "10"

// since "ptr" points to var, that means "*ptr" IS var 


Conversely, let's try something else:

1
2
3
4
int* ptr = 0;  // a null pointer (points to memory address 0, which is "nothing")
int var = 5;

*ptr = var;  // access violation -- program crash! 


The reason for this is because you're trying to take the variable 'var' and assign it to whatever variable ptr points to. But ptr doesn't point to anything! It points to bad memory.

EDIT: ninja'd =(
Last edited on
Oh sweet, another shortcut (:

Thank you hanst &disch for your replies! Marking as solved.
Last edited on
Topic archived. No new replies allowed.