|
From: Green, A. <JA...@GA...> - 2001-12-29 01:18:48
|
This may be OT, but I know I've seen programming questions here before, so I'll give it a try.
I wrote up a small program that tries to explain how pointers work to me.
#include <iostream>
double* f(double *pD2)
{
*pD2 = 15;
return pD2;
}
int main()
{
double d;
double *pD = &d;
d = 20;
*pD = 10;
std::cout << *pD << '\n';
std::cout << d << '\n';
double *pD3 = f(&d);
std::cout << d << '\n';
std::cout << *pD3 << '\n';
d = 100;
std::cout << d << '\n';
std::cout << *pD3 << '\n';
std::cout << *pD << '\n';
return 0;
}
The last parts of the output are 100 100 100. My question is: Why does *pD3 change when it points to a function that changes whatever is at &d ? I thought calling cout would run that function again, or at least give me the same value as before.
|