Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » need help with declaring a 2d int array

This thread is locked; no one can reply to it. rss feed Print
 1   2   3 
need help with declaring a 2d int array
William Labbett
Member #4,486
March 2004
avatar

hi all,

i'm miffed as to why this doesn't work. i get about 20 compile errors
when i compile, which aren't helping.

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 ?

1 
2#define LARGEST_SHADING_CIRCLE_DIAMETER 520
3#define SMALLEST_SHADING_CIRCLE_DIAMETER 192
4#define DIAMETER_DECREMENT =\
5 (LARGEST_SHADING_CIRCLE_DIAMETER - SMALLEST_SHADING_CIRCLE_DIAMETER)\
6 /10
7 
8int circle_sizes[10] = {
9 LARGEST_SHADING_CIRCLE_DIAMETER,
10 LARGEST_SHADING_CIRCLE_DIAMETER - (1 * DIAMETER_DECREMENT),
11 LARGEST_SHADING_CIRCLE_DIAMETER - (2 * DIAMETER_DECREMENT),
12 LARGEST_SHADING_CIRCLE_DIAMETER - (3 * DIAMETER_DECREMENT),
13 LARGEST_SHADING_CIRCLE_DIAMETER - (4 * DIAMETER_DECREMENT),
14 LARGEST_SHADING_CIRCLE_DIAMETER - (5 * DIAMETER_DECREMENT),
15 LARGEST_SHADING_CIRCLE_DIAMETER - (6 * DIAMETER_DECREMENT),
16 LARGEST_SHADING_CIRCLE_DIAMETER - (7 * DIAMETER_DECREMENT),
17 LARGEST_SHADING_CIRCLE_DIAMETER - (8 * DIAMETER_DECREMENT),
18 SMALLEST_SHADING_CIRCLE_DIAMETER
19};

compile errors available if wanted.

:-/

23yrold3yrold
Member #1,134
March 2001
avatar

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. ;D

Your DIAMETER_DECREMENT declaration looks a bit suspect. I really do hate macros ...

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

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 the red "x" that closes a window, really isn't red, but white on red background.

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
avatar

Lose the '=' in the third macro.

William Labbett
Member #4,486
March 2004
avatar

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
code on 'simple C question' thread.

(i shouldn't use your brains as my compiler i suppose :P)

i thought the '=' was out of place (no other defines need one)

hope it works..

23yrold3yrold
Member #1,134
March 2001
avatar

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. :P)

All three could be const int's, since the third macro only gets calculated once and doesn't change anyway ...

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

Rash
Member #2,374
May 2002
avatar

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:
http://edu.loviisa.fi/~jhalmen/images/marvin.jpg
That's better

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Years of thorough research have revealed that the red "x" that closes a window, really isn't red, but white on red background.

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
avatar

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.

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

Rash
Member #2,374
May 2002
avatar

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?
[/EDIT]

23yrold3yrold
Member #1,134
March 2001
avatar

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. ::) I saw the '=' later.

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

X-G
Member #856
December 2000
avatar

Rash: The pseudo-Finn asked, so I answered. ::)

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

Yves Rizoud
Member #909
January 2001
avatar

#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?
Good, bad, I'm the guy with the gnu.

Rash
Member #2,374
May 2002
avatar

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
avatar

Quote:

Make a mistake in declaring one, see the mistake somewhere else.

That is so very annoying. ;D

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;
   }

;)

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

Yves Rizoud
Member #909
January 2001
avatar

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
Gillius's Programming -- https://gillius.org/

spellcaster
Member #1,493
September 2001
avatar

Geez. Since when are macros evil?
You just have to know when to use them.

--
There are no stupid questions, but there are a lot of inquisitive idiots.

ReyBrujo
Moderator
January 2001
avatar

Yes, I agree with gillius. Even if you bypass the const protection, the code at runtime will crash.

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

23yrold3yrold
Member #1,134
March 2001
avatar

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

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

spellcaster
Member #1,493
September 2001
avatar

As long as there's no way to replace the # and ## macro operations, there's a need for defines. It's that easy.

--
There are no stupid questions, but there are a lot of inquisitive idiots.

23yrold3yrold
Member #1,134
March 2001
avatar

Not familiar with what you mean. And bluntly, it's definitely not relevant to the code in the original post. :)

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

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
Gillius's Programming -- https://gillius.org/

ReyBrujo
Moderator
January 2001
avatar

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 :)

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

Richard Phipps
Member #1,632
November 2001
avatar

How often do you use MIN, MID and MAX? I find them very useful. :)

 1   2   3 


Go to: