Search code examples
arrayscc-stringsstring-comparison

Error: "Excess elements in char array initializer"


As you can see, I am trying to check whether the element entered by the user is present in my array or not. But the following errors occur on the line on which arr is declared and defined:

1)[Error] excess elements in char array initializer 
2)[Error] (near initialization for 'arr')

This is my code:

#include <stdio.h>

int main(){
    char word;
    scanf("%d",&word);
    char arr[7]={"break", "case", "continue", "default", "else", "defer", "for"};
    int i,n=7;
    for(i=0;i<n;i++){
        if(word==arr[i]){
            printf("Keyword Found");
        }
        else{
            printf("Keyword Not Found");
        }
    }
}

Solution

  • The variable arr is an array of char's, but the initialization list consists of an array of string literals. A string literal can be used to initialize an array of char's, but that is not what arr is, hence the compiler producing an error regarding excess elements. These statements would be valid, as the initializer contains a single string literal:

    char arr[6] = { "break" };
    

    or rather

    char arr[6] = "break";
    

    Change arr to be an array of an array of char's and the code will compile:

    char arr[7][9]={"break", "case", "continue", "default", "else", "defer", "for"};
    

    These are 7 elements of which the longest has the length 9: "continue". Note that this number is the 8 letters of the word, and 1 for the null-terminator (\0). Another way of initializing arr is this:

    char *arr[7] = { /* ... */ };
    

    In this case, arr contains 7 char pointers to the 7 strings (which exist elsewhere in memory).

    Here is your full program with line-by-line corrections and suggestions:

    #include <stdio.h>                         // Add stdio header for scanf and printf
    #include <string.h>                        // Add string header for strcmp
    
    int main(void){                            // Add "void" for a correct main signature
        char word[64];                         // Needs to be a character array, not a single character
        scanf("%64s", word);                   // Needs to read a string of max 64 characters, not an integer
    
                                               // Next line needs to be a 2D array: it is an array of character arrays
    
        char arr[7][9]={"break", "case", "continue", "default", "else", "defer", "for"};
    
        int n=7;                               // Declare variables on separate lines
        for(int i=0;i<n;i++){                  // Declare/define i within the for-loop to reduce its scope
            if(!strcmp(word, arr[i])){         // Replace == by strcmp
                printf("Keyword Found\n");     // Add newline character for nicer output
            }
            else{
                printf("Keyword Not Found\n"); // Add newline character for nicer output
            }
        }
    }
    

    Note that, to make it work, also the type of word, the arguments to scanf and the string comparison were changed. The == compares if the two pointers (to the two strings) are equal (in other words, the two pointers point to the same location), but does not compare the contents of the two strings. See here for more info strcmp. Note that it returns 0 if the strings are identical.

    The next step may be to scan the list of keywords to detect whether a match was found and only print one "found / not found" statement at the end. This can be done by using a boolean:

    #include <stdbool.h>
    #include <stdio.h>
    #include <string.h>
    
    int main(void){
        char word[64];
        scanf("%64s", word);
        char arr[7][9]={"break", "case", "continue", "default", "else", "defer", "for"};
        int n=7;
        bool keywordFound = false;
        for(int i=0;i<n;i++){
            if(!strcmp(word, arr[i])){
                keywordFound = true;
                break;
            }
        }
    
        if (keywordFound)
            printf("Keyword Found\n");
        else
            printf("Keyword Not Found\n");
    }