Is there a work around for invalid octal digit in an array?

Go To StackoverFlow.com

1

I'm trying to create an array which will hold the hours in a day so I can loop through it for a clock.

I have:

int hourArray[24] = {12, 01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12, 01, 02,
                     03, 04, 05, 06, 07, 08, 09, 10, 11};

I am getting the error on the following numbers in order 08, 09, 08, 09.

It tells me:

Error: invalid octal digit

I've never run into this before and I'm wondering if there is any way around it?

2012-04-04 23:23
by sircrisp
Literals beginning with 0 are considered by the compiler to be octal, align them with spaces instead - Mooing Duck 2012-04-04 23:33


7

Sure: don't use leading 0s when you don't mean octal:

int hourArray[24] = {12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 
   1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};

Don't forget that you're only specifying the numbers in the array - not any particular text representation of the number. (So if you write 012 that's equivalent to writing 10 - you'll end up with the same number.) If you want to format those numbers with a leading 0 later that's an entirely different aspect of the code.

2012-04-04 23:25
by Jon Skeet
What if you want the number zero but you don't mean octal - Kerrek SB 2012-04-05 00:12
@KerrekSB: (1 - 1) - Jesse Good 2012-04-05 00:20
@KerrekSB : just 0 would be fine instead of 0 - keety 2012-04-05 00:35
Ads