#define, const rules in C
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]
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).
Unless this is some sort of gcc specific syntax, don't you mean
const [type] XMIN = ...;
or
#define XMIN ...
const [type] XMIN = ...;
or
#define XMIN ...
Uh...I'm not exactly sure what you're trying to do here, or what your problem is, but maybe this helps..
or
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];
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];
#define N ...
#define XMIN = ...
#define XMAX = ...
const double XINCR = (XMAX-XMIN)/N ***
double arr[N];
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...
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement