|
|
| need help with declaring a 2d int array |
|
William Labbett
Member #4,486
March 2004
|
hi all, i'm miffed as to why this doesn't work. i get about 20 compile errors i think you should be able to see what i'm trying to do but can you help me with how to do it properly ?
compile errors available if wanted.
|
|
23yrold3yrold
Member #1,134
March 2001
|
Quote: compile errors available if wanted.
Duh. I'm guessing wildly here, that the compiler doesn't like the ides of you initially constructing the array with values that have to be calculated or something? I have no idea, really. Your DIAMETER_DECREMENT declaration looks a bit suspect. I really do hate macros ... -- |
|
Johan Halmén
Member #1,550
September 2001
|
int circle_sizes[10] = { LARGEST_SHADING_CIRCLE_DIAMETER, That happens at compiling time and the compiler doesn't calculate stuff like: LARGEST_SHADING_CIRCLE_DIAMETER - (1 * DIAMETER_DECREMENT), or does it? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest. |
|
Rash
Member #2,374
May 2002
|
Lose the '=' in the third macro. |
|
William Labbett
Member #4,486
March 2004
|
yeah that's probably it i guess. can't see why a compiler can't add a few ints together tho' it's nicer for me because this way facilitates easily trying different values. i'll swap the sc's for numbers and see if it's more digestable
Quote: Lose the '=' in the third macro.
will do noone mentioned it being wrong earlier when i posted that (i shouldn't use your brains as my compiler i suppose i thought the '=' was out of place (no other defines need one) hope it works..
|
|
23yrold3yrold
Member #1,134
March 2001
|
Quote: can't see why a compiler can't add a few ints together tho'
Can't see why you can't declare your macro properly (and really, the only way to properly define a macro is to use something else. All three could be const int's, since the third macro only gets calculated once and doesn't change anyway ... -- |
|
Rash
Member #2,374
May 2002
|
Quote: I'm guessing wildly here, that the compiler doesn't like the ides of you initially constructing the array with values that have to be calculated or something? Since the resulting value can be arrived at at compile-time, where's the problem? |
|
Johan Halmén
Member #1,550
September 2001
|
How much can a compiler calculate? I've never used anything else than constants in that kind of code lines. [OT] Cheer up, Marvin: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest. |
|
X-G
Member #856
December 2000
|
Any compiler worth its salt will optimize constant operations together into a single constant when compiling. So if you type "640 - 43" in code, it won't actually do a subtraction whenever that's used; the compiler will just put the constant 596 in the program instead. -- |
|
Rash
Member #2,374
May 2002
|
If you want a taste of what is possible, do a search for "template metaprogramming". [EDIT] X-G said: Any compiler worth its salt will optimize constant operations together into a single constant when compiling. So if you type "640 - 43" in code, it won't actually do a subtraction whenever that's used; the compiler will just put the constant 596 in the program instead.
Thank you, Captain Obvious. Why do you think only computations involving constant values are allowed in standard code? |
|
23yrold3yrold
Member #1,134
March 2001
|
Quote: Since the resulting value can be arrived at at compile-time, where's the problem?
I didn't see one; that's why it was a wild guess. -- |
|
X-G
Member #856
December 2000
|
Rash: The pseudo-Finn asked, so I answered. -- |
|
Yves Rizoud
Member #909
January 2001
|
#define LARGEST_SHADING_CIRCLE_DIAMETER 520 #define SMALLEST_SHADING_CIRCLE_DIAMETER 192 #define DIAMETER_INCREMENT ((LARGEST_SHADING_CIRCLE_DIAMETER - SMALLEST_SHADING_CIRCLE_DIAMETER) / 10) #define CIRCLE_SIZE(a) (SMALLEST_SHADING_CIRCLE_DIAMETER + a * DIAMETER_INCREMENT ) This should work ok. Note that I got CIRCLE_SIZE() backwards, but I found that more readable (0 is smallest, 10 biggest) edit: what, macros are bad? |
|
Rash
Member #2,374
May 2002
|
You know, this thread perfectly shows why macros are pure evil. Make a mistake in declaring one, see the mistake somewhere else. |
|
23yrold3yrold
Member #1,134
March 2001
|
Quote: Make a mistake in declaring one, see the mistake somewhere else.
That is so very annoying. Here's the only cool use of macros: // arthurmath.cpp #include <iostream> #define SIX 1 + 5 #define NINE 8 + 1 int main() { std::cout << SIX * NINE << std::endl; }
-- |
|
Yves Rizoud
Member #909
January 2001
|
Hey, I may have a roundoff error, but I did surround my numerical expressions with braces. make it #define MAX_RADIUS 520 #define MIN_RADIUS 192 #define CIRCLES 10 #define CIRCLE(a) ( (MAX_RADIUS - MIN_RADIUS) * (a) / CIRCLES + MIN_RADIUS )
|
|
gillius
Member #119
April 2000
|
Macros are evil. Use const for variables, inline functions for functions. Macros are evil. Macros are evil. evil Gillius |
|
spellcaster
Member #1,493
September 2001
|
Geez. Since when are macros evil? -- |
|
ReyBrujo
Moderator
January 2001
|
Yes, I agree with gillius. Even if you bypass the const protection, the code at runtime will crash. -- |
|
23yrold3yrold
Member #1,134
March 2001
|
Quote: You just have to know when to use them. Which is not oftern. 19 times out of 20, there's a better way to accomplish what you're doing. I don't use macros unless I can't find some other safer/faster/easier option ... and I usually can ... -- |
|
spellcaster
Member #1,493
September 2001
|
As long as there's no way to replace the # and ## macro operations, there's a need for defines. It's that easy. -- |
|
23yrold3yrold
Member #1,134
March 2001
|
Not familiar with what you mean. And bluntly, it's definitely not relevant to the code in the original post. -- |
|
gillius
Member #119
April 2000
|
I never said macros are useless. I also never said they should never be used. They are just evil. All preprocessor stuff is evil, including the header guards. An evil can be a necessary evil. Now, in this case, I think he should be using const variables and not defines. Gillius |
|
ReyBrujo
Moderator
January 2001
|
OT: 23, # is the stringtificator(sp?) operator, converting the parameter of a macro into a string, while ## is the concatenator, in case you didn't know -- |
|
Richard Phipps
Member #1,632
November 2001
|
How often do you use MIN, MID and MAX? I find them very useful. |
|
|
|