- Oct 27, 2007
- 17,009
- 5
- 0
I've been studying Java for a year so I have no problems with reference variable but I'm getting all mixed up with pointers. It's the syntax that is mixing me up and I'm hoping someone here can correct my thinking.
Here's an example of some code from the book I'm studying from (sorry about the formatting, code box is broken):
void main()
{
double input = 12.4
round (input);
cout << "Rounded value: " << input;
}
void round (double &num)
{
double frac, val;
frac = modf(num, &val);
num = val;
}
Here's how I'm thinking about the code. The main method declares a double variable and assigns the value 12.4 to it. The round method takes a reference variable as a parameter, so when input is passed in as a argument the method actually takes &input, the memory address of input as a parameter. All of that is fine so far.
The modf() function is a standard library function whose signature looks like
modf(double num, double *i)
so the method takes a double and a pointer as arguments. I don't understand why &val is passed as the pointer argument. To my way of thinking, &val is not a pointer, it's just a memory address. How can a memory address be passed into an argument that requires a pointer?
I'm sure there's some fundamental problem with my way of thinking about pointers. Any help and tips would be greatly appreciated.
Here's an example of some code from the book I'm studying from (sorry about the formatting, code box is broken):
void main()
{
double input = 12.4
round (input);
cout << "Rounded value: " << input;
}
void round (double &num)
{
double frac, val;
frac = modf(num, &val);
num = val;
}
Here's how I'm thinking about the code. The main method declares a double variable and assigns the value 12.4 to it. The round method takes a reference variable as a parameter, so when input is passed in as a argument the method actually takes &input, the memory address of input as a parameter. All of that is fine so far.
The modf() function is a standard library function whose signature looks like
modf(double num, double *i)
so the method takes a double and a pointer as arguments. I don't understand why &val is passed as the pointer argument. To my way of thinking, &val is not a pointer, it's just a memory address. How can a memory address be passed into an argument that requires a pointer?
I'm sure there's some fundamental problem with my way of thinking about pointers. Any help and tips would be greatly appreciated.
