gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > Pass custom pointer to function.

#48440 - QuantumDoja - Wed Jul 20, 2005 12:04 am

Hi, I have the following structure: Question at bottom.

Code:

typedef struct combo {
   u8 step;
   u16 difference;
   u8 stepmax;
   u16 sequence[7];
}combo;


I have some variables of it like so:

Code:

combo combo0;
combo combo1;
combo combo2;


and a function:

Code:

void update_combo(combo cb, int id) {

   if (keyDown(cb.sequence[cb.step])) {
      cb.difference = 0;
      if (cb.step == cb.stepmax) {

         cb.step = 0;
      }
      cb.step++;
   } else {
      cb.difference++;
      if (cb.difference > 40) {
         cb.step = 0;
         cb.difference = 0;
      }
   }


}


How can I pass into the update_combo function the defined variables as a pointer, i have tried it, but it keeps saying that all the members of the struct are not a struct or union!!

I have tried this to no luck:

Code:


void update_combo(combo *cb, int id) {

   if (keyDown(*cb.sequence[*cb.step])) {
      *cb.difference = 0;
      if (*cb.step == *cb.stepmax) {

         *cb.step = 0;
      }
      *cb.step++;
   } else {
      *cb.difference++;
      if (*cb.difference > 40) {
         *cb.step = 0;
         *cb.difference = 0;
      }
   }


}

_________________
Chris Davis

#48443 - sajiimori - Wed Jul 20, 2005 1:13 am

Code:

*cb.sequence  // Equivalent to *(cb.sequence)
cb->sequence  // Equivalent to (*cb).sequence

Use the first if cb is a struct and sequence is a pointer. Use the second if cb is a pointer to a struct.

#48448 - APL - Wed Jul 20, 2005 2:34 am

Quote:
Code:
typedef struct combo {
   u8 step;
   u16 difference;
   u8 stepmax;
   u16 sequence[7];
}combo;


Are we confident this syntax is legal? Try it without the first "combo". (or without the second "combo" and do the typedef seperately.)


Notice that the first version of your function would not do anything usefull. If you pass by value like that you're working on a local copy of the struct, not the original. If you want to change the original, you need to pass by pointer, as in your second function.
_________________
-Andy L
http://www.depthchasers.com

#48450 - sajiimori - Wed Jul 20, 2005 3:06 am

The syntax is legal. Structs are in a different namespace than typedefs.

#48480 - QuantumDoja - Wed Jul 20, 2005 10:38 am

Thank you

you guys are teh best.

Here is the final code:

Code:


void update_combo(combo *cb, int id) {


   if (keyDown(cb->sequence[cb->step])) {
      cb->difference = 0;
      if (cb->step == cb->stepmax) {


         switch (id) {
            case 0:
               {
               //do something
               break;
               }
            case 1:
               {
               //do something
               break;
               }
            case 2:
               {
               //do something
               break;
               }
            default:break;
         }         


         cb->step = 0;
      }
      cb->step++;
   } else {
      cb->difference++;
      if (cb->difference > 40) {
         cb->step = 0;
         cb->difference = 0;
      }
   }

}

_________________
Chris Davis

#48502 - tepples - Wed Jul 20, 2005 1:49 pm

Step? Combo? Are you trying to infringe Konami's patent on DDR? Or are you using some other display method, such as the Taiko Drum Master method (50% Flip, horizontally scrolling) or the Parappa method (similar, but with a moving receptor over multiple rows of step marks)?

EDIT: Based on information you sent in a PM, you must mean "combo" as in Street Fighter, not "combo" as in Dance Dance Revolution. The word "step" confused me.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.