I want to update variables relating to velocities in a different function to where it is declared. But am not fully understanding the syntax for it.
I have the prototype as follows:
void moveBall(GOval ball, double* velocity);
And my function looks like this:
void moveBall(GOval ball,double* velocity)
{
int ballWidth = RADIUS*2;
// move ball along x-axis
move(ball, velocity, 0);
// bounce off right edge of window
if (getX(ball) + ballWidth >= WIDTH)
{
velocity = -velocity;
}
// bounce off left edge of window
else if (getX(ball) <= 0)
{
velocity = -velocity;
}
}
My variable velocity
is defined in main
like so:
int main(void)
{
//other unrelated code
//ball is also defined here
// set initial velocity
double velocity = 2.0;
while(true)
{
//stuff here
//move the ball
moveBall(ball, velocity);
pause(15);
}
I end up with this error for the function move:
error: passing 'double' to parameter of incompatible type 'double *'; take the address with & moveBall(ball, velocity);
Fairly sure i've made a mistake with understanding how to pass the variable's location rather than a copy
of it. As i want to update the value in moveBall
without having to return the value. So i thought pointers would be the way.. but it's not working - any guidance?
breakout
, I would only suggest that you don't create your own moveBall function and simply handle the ball movement in main.velocity
inside your function and have it actually change the one in main, unless you want to return the value to main. In my opinion, moving the ball and setting the velocity based on the ball's position are two separate actions. I have a function that moves the paddle, because it needs to take into account various things (where it is, etc.) but it doesn't need to change any values in main.