Archived

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

Dynamic Pointer arrays

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

Recommended Posts

Hello, I have been programming, C'', as a hobby for several years now and I have recently started an isometric-tile-based war game. I have linked lists that contain structs(UNIT) for each combat unit on both sides, with a new root node for each team and I need what I like to call a radar'' that is an array of pointers to UNIT structs, though it does not matter which team a UNIT is on, just if a UNIT is occupying any given coordinate. So, my question is that I do not work with a lot of pointers-to-pointers, but I need to dynamically allocate the radar'' array at the beginning of each game, because the actual physical map that the game is played on can be different sizes. And, I think that this initialization is correct: UNIT **radar; A pointer to an array of pointers. Now, my problem is that I cannot quite get the syntax for the rest of the time I want to use the radar.'' For example, I want to intialize the entire map to NULL: for( x = 0; x < size_x; x++ ) for( y = 0; y < size_y; y++ ) radar[x * size_x + size_y] = NULL; <-- Problem! How do you syntactically set up `radar'', a pointer to an array of pointers to UNITs, so I can change the value(address) of each coordinate in the map, i.e. point it to NULL for no unit, or to the address of the coord''s occupier. I would be much obliged for any help. Colin

Share on other sites
hmm first try to use a ZeroMemory instead of two for loops, it is much faster.

Share on other sites
Look ... you forgot to allocate those pointer ...

for( x = 0; x < size_x; x++ )
for( y = 0; y < size_y; y++ )
radar[x * size_x + size_y] = NULL; <-- Problem!

Remember to delete those afterward ...

If you already know the size of your pointer array, you better not allocate them dynamicaly ...

#define size_x 10
#define size_x 11

Anyhow it is up to you ;P

“ ZIM: But... Invader''s blood marches through my veins, like giant radioactive rubber pants! The pants command me! Do not ignore my veins! “

• Forum Statistics

• Total Topics
628757
• Total Posts
2984530

• 12
• 25
• 12
• 10
• 17