# #define, const rules in C

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

## 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 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 on other sites
Yeah that code doesn't make any sense, did you mean int instead of define?

##### Share on other sites
Unless this is some sort of gcc specific syntax, don't you mean

const [type] XMIN = ...;

or

#define XMIN ...

##### 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)Ndouble arr[N];

##### 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 on other sites
Quote:
 Original post by cointossI 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 on other sites
Quote:
 Original post by cointossI 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.

• 38
• 12
• 10
• 10
• 9
• ### Forum Statistics

• Total Topics
631362
• Total Posts
2999577
×