1

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?

7
  • If this is for breakout, I would only suggest that you don't create your own moveBall function and simply handle the ball movement in main.
    – curiouskiwi
    Commented Aug 4, 2015 at 1:34
  • Oh. How come? - it's kinda messy when it's all in main i find.
    – user7869
    Commented Aug 4, 2015 at 2:30
  • you are essentially moving 4 lines of code from main and creating a function. To me, that's overcomplicating it. Also, you are running into the fact that you can't change the 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.
    – curiouskiwi
    Commented Aug 4, 2015 at 4:27
  • Its not four lines - the ball has to check edge collisions and blocks and paddle collisions thats a lot of lines - if its moved to a function it feels cleaner.
    – user7869
    Commented Aug 4, 2015 at 4:29
  • you're putting all that into moveBall()?
    – curiouskiwi
    Commented Aug 4, 2015 at 4:34

2 Answers 2

1

Just to actually answer the question, as you've seen, variable arguments are passed as values, and any changes you make in the function will not be reflected in the original variable. However, if you pass a pointer to the variable, then you can make changes to the original variable.

The declaration of your function is fine and requires no changes:

void moveBall(GOval ball, double* velocity)

But to make sure that you are actually using this pointer, you would have to deference that velocity pointer in all your uses of it in your function definition (ie, use *velocity in all places where you have velocity). This will reference the original value.

Then, when you call the function from main, you need to pass it the address of your velocity variable (ie, its pointer), like moveBall(ball, &velocity);

An alternative way to do it, given that the velocity variable is only ever set once in main and then entirely handled by your function, is to use a static variable static double velocity = 2.0; in your function. A static variable is like a global but only within the scope of that function. The variable retains its value from one call to the next. But what it means is that you aren't having to deal with pointers to velocity declared in main, simply so you can keep track of the value when calling your moveBall() function.

You would have something like:

void moveBall(GOval ball)
{
    static double velocity = 2.0;  //set default
    move(ball, velocity, 0);

    // do the other stuff just as you did above
}

then, in main(), you would simply moveBall(ball);

The use of static function variables isn't really covered in CS50! :)

1

I'm with @curiouswiki that it is not worth having a separate function for this. however, the problem is exactly as the error message states. that is, you're passing an argument of type double where you're supposed to be passing a double *.

clearly this is about the variable named velocity.

I'm assuming that you want to update velocity inside moveBall. this can be achieve through a pointer to it. however, the SPL's move, doesn't take a double * as a second argument and you're not updating velocity's value correctly in there.

watch the short on pointers for more info!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .