question about declaration precedence

This is a discussion on question about declaration precedence within the C Programming forums, part of the General Programming Boards category; hello, can anyone explain to me why when i declare this array: Code: int myArray[] = {0-9}; before this array ...

1. question about declaration precedence

hello,

can anyone explain to me why when i declare this array:

Code:
` int myArray[] = {0-9};`
before this array of pointers to strings:

Code:
` char *engNumbers[] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };`
that i get (null) values for the strings, but if i declare the array of pointers to strings first it works properly?

here is the non-working code:

Code:
```#include <stdio.h>
#include <string.h>

int main()
{
int myArray[] = {0-9};

char *engNumbers[] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };

int h, i, j, k;

int myProb = 17150;

for (i = 0; i < 10; i++)   // initialize the array
{
myArray[i] = 0;
}

for (h = 0; h < 10; h++)
{
printf("%s\n", engNumbers[h]);
}

while (myProb != 0)
{
j = (myProb % 10);  // get the modulus and
myArray[j]++;       // assign it to the proper array element
myProb = (myProb / 10);  // do the division so you can move on
}

for (k = 0; k <= 9; ++k)
{
if (myArray[k] == 1)
printf("There is one %d in the number.\n", k);
else if (myArray[k] > 1)
printf("There are %s %d's in the number.\n", engNumbers[k + 1], k);
}

return 0;
}```
and here is the working code:

Code:
```#include <stdio.h>
#include <string.h>

int main()
{
char *engNumbers[] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };

int myArray[] = {0-9};

int h, i, j, k;

int myProb = 17150;

for (i = 0; i < 10; i++)   // initialize the array
{
myArray[i] = 0;
}

for (h = 0; h < 10; h++)
{
printf("%s\n", engNumbers[h]);
}

while (myProb != 0)
{
j = (myProb % 10);  // get the modulus and
myArray[j]++;       // assign it to the proper array element
myProb = (myProb / 10);  // do the division so you can move on
}

for (k = 0; k <= 9; ++k)
{
if (myArray[k] == 1)
printf("There is one %d in the number.\n", k);
else if (myArray[k] > 1)
printf("There are %s %d's in the number.\n", engNumbers[k + 1], k);
}

return 0;
}```
i'm using gcc 4.0.1.

thanks!

2. What exactly is
Code:
`int myArray[] = {0-9};`
...supposed to do? It looks to me like it would create a one-element array with the value -9. I thought array initializers had to be constants though. Hmmmm...

It certainly won't create:
myArray[0] = 0;
myArray[1] = 1;
...
myArray[9] = 9;

...like it looks like you're trying to make it do. If you want an un-initialized 10-element array then declare it like:
Code:
`int myArray[10];`

3. thank you itsme,

i knew it was something silly i was doing.

4. Was that just a wild guess that 0-9 would work or are you a FORTRAN programmer or something?

5. hi sly,

i think i just got confused between declaring an array and declaring an array of pointers. i wanted to make sure to get the array of pointers correct, so i think i was paying less attention when i made the actual array of integers.

what more can i say? i'm certainly not a FORTRAN programmer.

Popular pages Recent additions