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.

C/C++ > Wierd Array Error

#2493 - GbaGuy - Tue Feb 04, 2003 11:40 pm

When I go like this:

VRAM[0x560] = 0xFF00;

The compiler goes like this:

6: invalid types `int[int]' for array subscript

Why?

VRAM is defined as:

#define VRAM (u16*)0x6000000

Any help would be extremely appreciated.

#2501 - tepples - Wed Feb 05, 2003 1:16 am

What's happening is operator precedence interacting in initially unexpected way with your macro.

Your code:
#define VRAM (u16*)0x6000000
VRAM[0x560] = 0xFF00;

Expands inside the preprocessor to this:
(u16*)0x6000000[0x560] = 0xFF00;

Which the expression parser turns into this:
(u16 *)(0x6000000[0x560]) = 0xff00;

In an a[b] expression, one of a or b must be a pointer, and the other must be an integer. (Trivia: a[b] means the same as b[a].) But in your expression, a and b are both integers, hence the error about invalid int[int].

To fix this, put parentheses around your macro:
#define VRAM ((u16*)0x6000000)
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#2504 - GbaGuy - Wed Feb 05, 2003 2:05 am

Wow, thanks a lot!

I would have never (well, maybe eventually) figured
that out myself.