Is it possible to use integers as names for arrays?

998 views Asked by At

I'm porting a game from the Sega Genesis to the Sega Saturn, and limited to C. I'm also following how the game is coded in Motorola 68000 assembly code as closely as possible, which requires many, many arrays of identical structures. I'm talking upwards of 300 or more. For the sake of organization and readability, it would be easier to name them with numbers instead of a string of characters

For example, I would prefer:

int 000[4] = {1, 2, 3, 4};
int 001[4] = {1, 2, 3, 4};

As opposed to:

int zerozerozero[4] = {1, 2, 3, 4};
int zerozeroone[4]  = {1, 2, 3, 4};

But my compiler won't compile unless the array name starts with a character. Is there a way to do this? I suppose I could just throw an a at the front of the arrays, but I'd like to avoid that if possible.

3

There are 3 answers

5
Edwin Finch On BEST ANSWER

It's not possible to do this, no.

However, you can begin the variable name with any valid character (such as a letter or underscore) and then make the rest numbers.

For example, instead of int 001[4] you could do int _001[4].

For people just reading my answer - shoutout to unwind who answered below on how this is not great design for human reading or usability. See and upvote his answer too for more details.

3
unwind On

This is very strange design.

A number is really not a name, in the human sense. Nobody reading the code will understand the meaning of an array called _031.

You should figure out what all of these arrays have in common, and use a single multi-dimensional array with a descriptive name.

So instead of

int _001[4];
int _002[4];
...
int _300[4];

you should have something like

int whatever[300][4];

Then wherever you used to access _002, you'd just access whatever[1] instead. Much cleaner. If you want to you can add one and make it 301 and use 1-based indexes.

1
Steve Fan On

No, and never.

Remember kids, the regex for identifiers (C-wise) is [_a-zA-Z][0-9a-zA-Z]*, and the regex for decimal numbers (C-wise) is [0-9]*! Your compiler can literally see 000 (etc.) as a decimal number and instead 'wrongfully' take it as an expression which will then create a massive compiler catastrophe!

EDIT: Why C disallow number prefixed identifier?

There's one important concept that C allowed suffix.

For example, consider this pseudo-code: int 01234; int 01234l;

If we modified the regex of identifiers to [_0-9a-zA-Z]*, not only that it will overlap the range of decimal numbers, it may also overlap the range of special suffix like l, ul, f or d too. We humans can sure read and group those thing, but computers can't! Computers cannot solve ambiguity or otherwise it will an eternal death!