Sign in to follow this  

#define, const rules in C

This topic is 3662 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 would like to calculate the value on the starred line given the following definitions: #define N ... const double XMIN = ...; const double XMAX = ...; const double XINCR = (XMAX-XMIN)/N *** double arr[N]; However, gcc complains about initializer elements not being constant. How should I rewrite this so it compiles? Thanks! Edit: sorry for typo, const define -> const double [Edited by - cointoss on December 3, 2007 7:39:13 PM]

Share this post


Link to post
Share on other sites
const define makes no sense. Either use #define XMIN ... or const int XMIN = ...; not both together (substitute whatever type is appropriate if XMIN isn't an int).

Share this post


Link to post
Share on other sites
Uh...I'm not exactly sure what you're trying to do here, or what your problem is, but maybe this helps..


const int N = 5;
const double XMIN = 1.0, XMAX = 100.0;
const double XINCR = (XMAX-XMIN)/(double)N;
double arr[N];



or


#define N 5
#define XMIN 1.0
#define XMAX 100.0
#define XINCR (XMAX-XMIN)/(double)N
double arr[N];

Share this post


Link to post
Share on other sites
I tried different combinations, and this is the correct way to do it in case anyone else has a similar problem:

#define N ...
#define XMIN = ...
#define XMAX = ...
const double XINCR = (XMAX-XMIN)/N ***
double arr[N];

Share this post


Link to post
Share on other sites
Quote:
Original post by cointoss
I tried different combinations, and this is the correct way to do it in case anyone else has a similar problem:

#define N ...
#define XMIN = ...
#define XMAX = ...
const double XINCR = (XMAX-XMIN)/N ***
double arr[N];


Depends on what you mean by "correct way". If you need XINCR as a constant later, then it's still incorrect. The thing is, in C, "const" doesn't mean "constant" it means "readonly". A "const double" is still a variable, just a variable that you can't change. Being a variable, it can't be used where C wants a constant.

I think a C programmer would be more likely to use yahatsu's second example (with XKNCR as a #define). yahatsu's first example doesn't compile in C.

C++ is a different story entirely...

Share this post


Link to post
Share on other sites
Quote:
Original post by cointoss
I tried different combinations, and this is the correct way to do it in case anyone else has a similar problem:

#define N ...
#define XMIN = ...
#define XMAX = ...
const double XINCR = (XMAX-XMIN)/N ***
double arr[N];

You wouldn't have those = symbols in those #defines of XMIN and XMAX of course.

Share this post


Link to post
Share on other sites

This topic is 3662 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