Jump to content
  • Advertisement
Sign in to follow this  
Real World

Macro problem with c

This topic is 5479 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I currently have an array whos values are defined when the array is declared. ie. int a[] = {1,2,3}; One of these elements is a macro CHTIME(m,s) that takes two integers and returns the following ((m)*60+(s))*FPS Until recently this worked fine as FPS was #define FPS 60. its now been changed to a variable and I get compile errors as FPS is no longer constant. I can't turn CHTIME into a function as it would not return a constant so what would be the simplest and most effective way to get this working again without multiplying everything where it is used by FPS?

Share this post

Link to post
Share on other sites
Original post by Maquiavel
Are you using C?
Because in C++ you can use constants:
int const FPS = 60;
and they fill correctly everywhere.

This would not solve his problem.
It's just another way to express a constant value.
What he wanted to achieve is initialising an array with a non-constant valu. This is not possible in either C or C++.

// works perfectly
const int FPS = 60;
int a[] = { 1, 2, FPS };

// will not work - FPS_nc is not a constant
int FPS_nc = getFps();
int b[] = { 1, 2, FPS_nc };

// this works.
int c[] = { 1, 2, 0 };
c[2] = FPS_nc;

Share this post

Link to post
Share on other sites
solution :

#define CHTIME(m,s) ((m)*60+(s))*FPS
const int newFPS = CHTIME(m,s) ; // this assumes m and s are given before it reaches the declaration/initialization of the const int newFPS

int a[] = {1, 2, newFPS} ;

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!