Subscripted value is neither array nor pointer ?

This is a discussion on Subscripted value is neither array nor pointer ? within the C Programming forums, part of the General Programming Boards category; /*What is wrong with my code that is causing the compiler */ /*to say that the "subscripted value is neither ...

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    1

    Exclamation Subscripted value is neither array nor pointer ?

    /*What is wrong with my code that is causing the compiler */
    /*to say that the "subscripted value is neither array nor*/
    /*pointer"? I believe it is referring to the variable count */
    /*in the function body of display_total_count but I don't */
    /*understand what I can change to make it run*/

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void character_recognition_and_count(int input);
    void display_total_count(int array);
    
    int main(void)
    {
    
        char input[81] = "Jose was here and left.";
    
    
        character_recognition_and_count(input[0]);
    
    
        return 0;
    }
    
    void character_recognition(int input[0])
    {
    
       
        static int count;
        int array[28] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
    
    
    
            for(count = 0; count <= 80; count++);
                {
    
    
                    switch(input[count])
                    {
                    case 'a':
                        array[0] = array[0] + 1; 
                        break;
                    case 'b':
                        array[1] = array[1] + 1;
                        break;
                    case 'c':
                        array[2] = array[2] + 1;
                        break;
                    case 'd':
                        array[3] = array[3] + 1;
                        break;
                    case 'e':
                        array[4] = array[4] + 1;
                        break;
                    case 'f':
                        array[5] = array[5] + 1;
                        break;
                    case 'g':
                        array[6] = array[6] + 1;
                        break;
                    case 'h':
                        array[7] = array[7] + 1;
                        break;
                    case 'i':
                        array[8] = array[8] + 1;
                        break;
                    case 'j':
                        array[9] = array[9] + 1;
                        break;
                    case 'k':
                        array[10] = array[10] + 1;
                        break;
                    case 'l':
                        array[11] = array[11] + 1;
                        break;
                    case 'm':
                        array[12] = array[12] + 1;
                        break;
                    case 'n':
                        array[13] = array[13] + 1;
                        break;
                    case 'o':
                        array[14] = array[14] + 1;
                        break;
                    case 'p':
                        array[15] = array[15] + 1;
                        break;
                    case 'q':
                        array[16] = array[16] + 1;
                        break;
                    case 'r':
                        array[17] = array[17] + 1;
                        break;
                    case 's':
                        array[18] = array[18] + 1;
                        break;
                    case 't':
                        array[19] = array[19] + 1;
                        break;
                    case 'u':
                        array[20] = array[20] + 1;
                        break;
                    case 'v':
                        array[21] = array[21] + 1;
                        break;
                    case 'w':
                        array[22] = array[22] + 1;
                        break;
                    case 'x':
                        array[23] = array[23] + 1;
                        break;
                    case 'y':
                        array[24] = array[24] + 1;
                        break;
                    case 'z':
                        array[25] = array[25] + 1;
                        break;
                    default:
                        array[26] = array[26] + 1;
                    }
    
                    while(count == 80 || input[count] == '\n')
                                {
                                    display_total_count(array[0]);
                                    exit(0);               
                                }
                }
    }
    
    void display_total_count(int array)
    {
        char alphabet;
        int count = 0;
    
            for(alphabet = 97; alphabet <= 122; alphabet++) 
            {
                printf("%c was used: ", alphabet);
    
                while(count <= 26) 
                        { 
                            printf("%d times\n", array[count]);  /*PROBLEM ON THIS LINE*/
                            count++;
                            break;
                        }
            }
    }

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,524
    You cannot subscript an integer.
    Code:
    void character_recognition_and_count(int input);
    void display_total_count(int array);
    Problems dressed in red.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Some syntax problems are "dressed in red", above, but I'm going to try and convince you that your program is a dress rehearsal, and a good learning experience for you.

    And welcome to the forum!


    But junk that program immediately (please). Before it multiplies!

    Here's how you want to approach this problem of char recognition and counting:

    get the string you want to count (and catalog), the chars in.

    remember that each char has a number value, which the computer uses.


    use the char number value as the index to the catalog[] array. The catalog array is type char, and has 126 elements in it: catalog[126].


    Then use a single for or while loop, to go through the string, and for every char in the string, you increment the value of catalog[charValue]. (You will have to see that catalog[] elements are all set to zero, before you get to this loop, of course).

    And now print out catalog[i] in either one or two loops, depending on how you want it displayed.

    Code:
    for(i='A';i<='Z';i++) {
       printf("%c: %d\n",i,catalog[i]);
    }
    The first time through the loop, you will print out A: 3, if there are three uppercase A's in the string being processed. That will repeat for all the uppercase letters.

    The above prints out the catalog array for the uppercase letters. How would you add another loop to change it so it printed out the lowercase letters?

    If you use this, your program will shrink to about 15 lines of code, roughly.

    These "char number values" are frequently called ASCII values or character set values. You can download an ASCII table from the net, very easily - google ASCII table to see what I'm talking about.

    Wasn't that just clever?
    Last edited by Adak; 08-08-2012 at 12:45 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. subscripted value is neither array nor pointer: 2 files
    By pochis40 in forum C Programming
    Replies: 6
    Last Post: 05-27-2011, 03:16 PM
  2. Replies: 1
    Last Post: 05-08-2010, 10:03 PM
  3. double subscripted array
    By newcuser in forum C Programming
    Replies: 2
    Last Post: 02-13-2010, 10:59 PM
  4. Replies: 9
    Last Post: 03-16-2009, 02:18 AM
  5. subscripted value is neither array nor pointer
    By new_to_c in forum C Programming
    Replies: 8
    Last Post: 04-01-2007, 02:43 PM

Tags for this Thread


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