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++ > does recognise anpercent "&" ? plus does switc

#177044 - blessingta@hotmail.co.uk - Fri Dec 02, 2011 1:25 pm

does c also recognise the and percent "&"?

//header
//sections with errors
Code:
extern bool find_valid_movepath (unsigned int & uiPiece,/*find piece chosen to be moved*/unsigned char & Move_Piece,/*Locate valid path & make move if true*/unsigned char & pathway,unsigned char Store_valid_moves[9], unsigned char & validpos);


//c file
Code:
bool find_valid_movepath (unsigned int & uiPiece,/*find piece chosen to be moved*/unsigned char & Move_Piece,/*Locate valid path & make move if true*/unsigned char & pathway,unsigned char Store_valid_moves[9], unsigned char & validpos);


Code:
//whist valid position present
      switch (validpos)
      {
      case 0:
      //move invalid
         return false;
      //...
      break;
      default:
      //valid move found: now chose
      uiPiece = (rand() % validpos);

      //valid move was true
      valid_move_true (Move_Piece,
      //extract valid path
      Store_valid_moves[uiPiece]);
      return true;
      break;
      }

Its throwing up these errors now
Quote:

game.c
arm-eabi-gcc -MMD -MP -MF /g/YEAR2/Console_Programming_CONPRG/Course_Work/gba/my_gba_game/build/game.d -g -Wall -O3 -mcpu=arm7tdmi -mtune=arm7tdmi -fomit-frame-pointer -ffast-math -mthumb -mthumb-interwork -I/g/YEAR2/Console_Programming_CONPRG/Course_Work/gba/my_gba_game/include -I/D/devkitPro/libgba/include -I/g/YEAR2/Console_Programming_CONPRG/Course_Work/gba/my_gba_game/build -c /g/YEAR2/Console_Programming_CONPRG/Course_Work/gba/my_gba_game/source/game.c -o game.o
In file included from g:/YEAR2/Console_Programming_CONPRG/Course_Work/gba/my_gba_game/source/game.c:1:0:
g:/YEAR2/Console_Programming_CONPRG/Course_Work/gba/my_gba_game/include/game.h:43:47: error: expected ';', ',' or ')' before '&' token
g:/YEAR2/Console_Programming_CONPRG/Course_Work/gba/my_gba_game/source/game.c: In function 'check_player_turn':
g:/YEAR2/Console_Programming_CONPRG/Course_Work/gba/my_gba_game/source/game.c:157:9: error: case label does not reduce to an integer constant
g:/YEAR2/Console_Programming_CONPRG/Course_Work/gba/my_gba_game/source/game.c:169:3: error: case label does not reduce to an integer constant
g:/YEAR2/Console_Programming_CONPRG/Course_Work/gba/my_gba_game/source/game.c: In function 'reset_turn_info':
g:/YEAR2/Console_Programming_CONPRG/Course_Work/gba/my_gba_game/source/game.c:344:15: warning: unused variable 'ucMove' [-Wunused-variable]
g:/YEAR2/Console_Programming_CONPRG/Course_Work/gba/my_gba_game/source/game.c:343:15: warning: unused variable 'ucLocation' [-Wunused-variable]
g:/YEAR2/Console_Programming_CONPRG/Course_Work/gba/my_gba_game/source/game.c:342:6: warning: unused variable 'bvalid_Piece' [-Wunused-variable]
g:/YEAR2/Console_Programming_CONPRG/Course_Work/gba/my_gba_game/source/game.c: At top level:
g:/YEAR2/Console_Programming_CONPRG/Course_Work/gba/my_gba_game/source/game.c:673:40: error: expected ';', ',' or ')' before '&' token
g:/YEAR2/Console_Programming_CONPRG/Course_Work/gba/my_gba_game/source/game.c: In function 'Randomly_Place_Piece':
g:/YEAR2/Console_Programming_CONPRG/Course_Work/gba/my_gba_game/source/game.c:711:6: warning: variable 'store_valid_moves' set but not used [-Wunused-but-set-variable]
g:/YEAR2/Console_Programming_CONPRG/Course_Work/gba/my_gba_game/source/game.c: In function 'ComputerAI_Move':
g:/YEAR2/Console_Programming_CONPRG/Course_Work/gba/my_gba_game/source/game.c:766:3: warning: statement with no effect [-Wunused-value]
g:/YEAR2/Console_Programming_CONPRG/Course_Work/gba/my_gba_game/source/game.c:768:3: warning: implicit declaration of function 'find_valid_movepath' [-Wimplicit-function-declaration]
make[1]: *** [game.o] Error 1
"make": *** [build] Error 2

> Process Exit Code: 2
> Time Taken: 00:01

#177046 - Miked0801 - Fri Dec 02, 2011 4:29 pm

"C" does not recognize reference passing, but C++ does. Or, if you are using a C only compiler, you pass by pointer instead of reference and use -> within your functions.

You appear to be using a GCC variant though. That is both a C and C++ compiler. Renaming your files from .c to .cpp will allow .cpp options to be used, but may cause other issues if you aren't up on C++ programming.

Also, style note, please do not use /* comments */ within the parameter list of your functions. Makes it dang hard to read and also has much room for accidental error. Even worse, don't use & within your comment blocks for and. That just hurts my head.

#177057 - Rajveer - Sat Dec 03, 2011 4:01 am

Don't mean to shift this topic but, how does a reference work behind the scenes? As in terms of where it's stored in memory, what it points to e.t.c.. Is it just like a pointer that the compiler handles automatically in some way?

I've never used C++ myself, and I'm trying to think of how pass by reference is more efficient than pass by value with a pointer and works without copying the address of the original variable into the reference, but references aren't variables are they, so how are they stored? :S

#177058 - Dwedit - Sat Dec 03, 2011 7:21 am

It's a pointer with different syntax.
When you first create a reference, it's like you are sticking the Address Of & operator in front of the right value.
Every time you use a reference, it's like you are sticking the dereference operator * in front of the reference.
References can not be reassigned.
_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."

#177061 - ant512 - Sat Dec 03, 2011 10:44 pm

Dwedit wrote:
References can not be reassigned.


Also, references cannot point at NULL.

#177062 - Miked0801 - Sat Dec 03, 2011 10:54 pm

There's alot more to it than that though. Head over to stackoverflow.com and read up a bit on it for further info.

#177064 - Dwedit - Sun Dec 04, 2011 6:39 am

ant512 wrote:
Dwedit wrote:
References can not be reassigned.


Also, references cannot point at NULL.


Why wouldn't references be able to point at NULL? This code seems to create a reference to NULL:
int &ref = *((int*)0);
The compiler happily accepts this code, and when you assign to ref, it writes to address 0.
_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."