#### Archived

This topic is now archived and is closed to further replies.

# #defines and a small problem..

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

## Recommended Posts

hello... i will try and keep this short and sweet... ive got a define: #define Chunk_Size 128 and i need to get the square root of that number... so i tried #define Row_Size sqrt(Chunk_Size) but that doesnt work.. im guessing because im using a function there.. so is there any way round this... i need to work out the sqrt in a define so i can use it in a 2d array in my classes public area: public: int Array[Row_Size][Row_Size]; am i doing it completly wrong or is there another way round this, i just need it so i can define a number and have this array within public be equal to the rows size.. which is the square root of chunk size..as you can see above.. Thanks to anyone who can help...

##### Share on other sites
Why not use constants......

const int Chunk_Size = 128;

int squared_size = sqrt(Chunk_Size);

[edited by - SumDude on February 28, 2004 9:53:15 PM]

##### Share on other sites
Your problem is probably that sqrt returns a double. Its very difficult to have an array with 11.313708498985X11.313708498985 dimensions. Make it a square. If it still doesn''t work try:

#define Row_Size (int)sqrt(Chunk_Size)

or better yet (squaring is MUCH faster than square-rooting,

#define Row_Size 8
#define Chunk_Size Row_Size*Row_Size

Zorx (a Puzzle Bobble clone)
Discontinuity (an animation system for POV-Ray)

##### Share on other sites
sorry i thought i had put in the (int) bit.. lol it would be funny if you could have an array of [0.3334343]... but yeah anyway i went with the row*row method... i just wanted to specify an overall size.... thanks for your help anyway ...

##### Share on other sites
quote:
Original post by SumDude
Why not use constants......

Could be useful in interfacing with C code. In C, "const" is more like "read only". It''s not actually a constant expression. I think the following produces a VLA in C99 (and is illegal in C89)

const size_t n = 10;
int array[n][n] = {0};

But I could be wrong. Also, I think clum''s

#define Row_Size (int)sqrt(Chunk_Size)

will fail for a similar reason in both C++ and C89 (producing a VLA in C99)

1. 1
2. 2
3. 3
Rutin
15
4. 4
khawk
13
5. 5
frob
12

• 9
• 9
• 11
• 11
• 23
• ### Forum Statistics

• Total Topics
633669
• Total Posts
3013257
×