I've got a program that I've got all coded out. It's just to demonstrate the basics of pointers but I can't get it to compile. I think there is just one problem I'm having but I can't figure it out. I've read articles and I feel like I'm doing it right.
________________________________
pBalance is already a pointer, &pBalance is now a pointer-to-a-pointer-to-a-float.
(You have some problems inside biggerBalance too, but I'll let you work them out. I suggest turning on compiler warnings. If using gcc, add -Wall to your compilation line).
So the way I understand it is the & gives me the address of that variable and * resolves the address to the data.
This is my revised code but I'm still getting the same issue. If I'm passing the pointer into biggerBalance with the * ... I don't know. I guess I'm not sure what I'm asking.
#include <iostream>
#include <iomanip>
using namespace std;
//biggerBalance:
//These lines do arithmetic, but don't store the return value anywhere.
*pBalance - date;
*pBalance - tax;
//main:
//This line will cause an error. pBalance is a pointer, there is no need to dereference it.
biggerBalance(dinner, movie, iceCream, *pBalance);
//These lines print the address of the variables.
cout << "Sam's balance: $" << &samsBalance << endl;
cout << "Sue's balance: $" << &suesBalance << endl;
awalrus00 wrote:
So the way I understand it is the & gives me the address of that variable and * resolves the address to the data.
When used as unary operators, yes; & returns the address of (pointer to) a variable, and * dereferences a pointer.
awalrus00 wrote:
If I'm passing the pointer into biggerBalance with the *
Actually, by using * on a float* you are dereferencing it, and passing in a float.
awalrus00 wrote:
Additionally in the void biggerBalance function, I could call *pBalance anything I want couldn't I?
If by that you mean any valid identifier not already defined in scope, then yes. (It does not need to have the same name as the variable/value being passed to it.)
[ code ] Please format your post by inclosing your code with tags... [ /code ]
Thank you! That was fantastic help. I didn't understand that putting the asterisk twice dereferenced it and turned it back into a variable. That's what was giving me the error. It was trying to pass it as a variable rather than just using the address of the pointer.
Additionally I was referencing the pointer when I just wanted to output their account balance in the last two cout statements.