Sign in to follow this  

Macro problem with c

This topic is 4836 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
Quote:
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++.
Example:

// 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

This topic is 4836 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this