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. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    18

    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. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    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];
    Last edited by itsme86; 06-16-2006 at 11:20 AM.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    18
    thank you itsme,

    i knew it was something silly i was doing.

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Was that just a wild guess that 0-9 would work or are you a FORTRAN programmer or something?
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    18
    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 subscribe to a feed

Similar Threads

  1. Question about a declaration
    By Boxknife in forum C Programming
    Replies: 3
    Last Post: 06-22-2008, 10:28 PM
  2. Precedence question
    By jw232 in forum C++ Programming
    Replies: 6
    Last Post: 06-04-2008, 01:23 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  4. Errors with including winsock 2 lib
    By gamingdl'er in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2005, 07:13 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21