Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with pointers

Status
Not open for further replies.

BorlandDave

Programmer
Jun 13, 2005
86
GB
I've got a pointer to a double set up like this:

double aValue;
double *aValuePointer;
aValuePointer = (double *) &aValue;

I can pass this pointer to a function which expects a double using aValuePointer[0]. But the problem is I want to copy this pointer to a new one. I thought I could just declare a new one and make it equal to the first one, but that doesn't work. This is what I tried:

double *aValuePointer2;
aValuePointer2 = aValuePointer;

I tried passing this to the same function using aValuePointer2[0], but I keep getting an error.

Can anyone help? Cheers.
 
Don't see why that wouldn't work.
[tt]
double aValue;
double*aValuePointer;
double*aValuePointer2;
void func(double);

aValuePointer=&aValue;
// There's no need to cast it, it's already a double* .

aValuePointer2=aValuePointer;

func(*aValuePointer2);
// aValuePointer2[0] would also work,
// but it would imply that you're accessing an array.
[/tt]
 
The error occurs on the line where I call the function with the new pointer. I get:

Project xxx.exe raised exception class EAccessViolation with message 'Access violation at address 0047A748 in module 'xxx.exe'. Read of address 00000008'. Process stopped. Use Step or Run to continue.

My program is actually more complex than the example I outlined in my first post. But a pointer is a pointer right? It shouldn't matter if it points to a value in a different cpp file should it?
 
In the debugger, get all the addresses for the various items and get the actual values pointed to and lets see what they are.
Are these items in a class?
How is all this declared?
 
Thanks c567591, I can't believe I didn't try that. I was trying to assign the second pointer before I'd initialised the first. Therefore the second pointer was always NULL.

Sorry for wasting everyones time :)
 
Np.
Sometimes you gotta be hit with a tree to see the forrest. :)
At least I do anyway, I get working too closely on something and completely miss the obvious.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top