gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Beginners > Strange problem with a pointer

#54282 - Ultima2876 - Sat Sep 17, 2005 6:38 pm

Code:
int *map_write_x = 0;
map_write_x = &columnx1;
for(loop = 0; loop < 18; loop++)   //update the tiles at the edge of the screen
                     {
                        ((u16*)map_vram)[(32 * loop) + columnx1] = ((u16*)map)[(map_width * loop) + *map_position_x];
                     }


That does exactly what I want it to do.

Code:
int *map_write_x = 0;
map_write_x = &columnx1;
for(loop = 0; loop < 18; loop++)   //update the tiles at the edge of the screen
                     {
                        ((u16*)map_vram)[(32 * loop) + *map_write_x] = ((u16*)map)[(map_width * loop) + *map_position_x];
                     }


But this doesn't. I want to have a pointer to columnx1, and reference the pointer instead of the variable itself. (This is because I'll have different columnx's and would rather just change the pointer than have multiple copies of the code). This is only the bit of the code with problems - the rest of the code is pretty big and would just clutter stuff if I put it in this post. The pointer is referenced in the rest of the code too - for example:

Code:
*map_write_x += 1;
                  *map_write_x = *map_write_x & 0x1F;


That actually does its job - adds 1 to columnx1, and if columnx1 goes above 31 it wraps it to 0.

But why can't I use the pointer to the variable in my for loop? Is there something I'm missing?

Thanks in advance =P

#54286 - poslundc - Sat Sep 17, 2005 6:52 pm

Your code looks fine, but the problem might not actually be there. Do some kind of debug-print or memory dump to see if the memory values you're getting for ((u16 *)map_vram)[(32 * loop) + columnx1] matches the memory values you're getting for ((u16 *)map_vram)[(32 * loop) + *map_write_x].

If you get the same values, it's an indicator that your problem is elsewhere in source, and this is just a symptom of the problem, not the source of the problem itself.

Dan.

#54289 - Ultima2876 - Sat Sep 17, 2005 7:13 pm

Aha. It turns out that for some reason I had columnx1 as a u8. I use ints all the time now, but I'm actually rehashing some code that I wrote when I was just starting out.

That helped enormously, thanks very much.

#54452 - Miked0801 - Mon Sep 19, 2005 5:13 pm

Reason 102849 not to use u8s unless you really really mean it :)