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!