-2

I get a error

expected initializer befor '/=' token.

I have

const unsigned in array[]

in my loop, I have:

for (int i = 0; i< length; i++)
{
     while (array[i] > 0)
     {
         const unsigned int array[i] /= 10;
     }
}

How can I fix it? Thanks!

Raging Bull
  • 18,593
  • 13
  • 50
  • 55
Quan
  • 79
  • 1
  • 2
  • 4

3 Answers3

4

const unsigned int array[i] /= 10;

should be:

array[i] /= 10;


If you write a type before a variable name, you perform a variable declaration. This is however not your intention here, you simple want to access it.

Sebastian Hoffmann
  • 11,127
  • 7
  • 49
  • 77
2

I suspect you intend to divide each array entry by 10. I also assume that your gave your array a size (in the brackets). I also assume that length is correct.

Still there are multiple errors.

First the array should be defined unsigned int instead of const unsigned in (remove the const and fix the typo) otherwise it can not be modified.

Then remove the type declaration in the loop and use array[i] /= 10; instead of const unsigned int array[i] /= 10;.

Additionally I wonder why you try to use two nested loops? Simply remove the while loop entirely:

for (int i=0; i<length; i++)
{
   array[i] /= 10;
}
Silicomancer
  • 8,604
  • 10
  • 63
  • 130
0

I think you have to read a little up on both arrays and in more general C. When you declare a variable with 'const' it declares it as an constant and therefor it cannot be changed later.

const unsigned int array[]
for (int i = 0; i < length; i++)
{
     while (array[i] > 0)
     {
         const unsigned int array[i] /= 10;
     }
}

Should be changed to

unsigned int array[];
for (int i = 0; i < length; i++)
{
     if (array[i] > 0)
     {
         // If array[i] is greater than zero, divide it by 10 
         array[i] /= 10;
     }
}

Ok that was the broad error fixing please checkout those links:

How to initialize all members of an array to the same value?

http://en.wikipedia.org/wiki/Constant_(programming)

Thanks.

Community
  • 1
  • 1
MrSykkox
  • 528
  • 4
  • 14