initializer-string for array of chars is too long

16,268

Solution 1

I think it's because there aren't any commas in your array initialization...

char array[num][length] = { "Becky Warre, 555-1223",
                            "Joe Looney, 555-0097",
                            "Geri Palmer, 555-8787",
                            "Lynn Presnell, 555-1212",
                            "Holly Gaddis, 555-8878",
                            "Sam Wiggins, 555-0998",
                            "Bob Kain, 555-8712",
                            "Tim Haynes, 555-7676",
                            "Warren Gaddis, 555-9037",
                            "Jean James, 555-4939",
                            "Ron Palmer, 555-2893" }

Solution 2

Seems you forgot to add comma's. Initializing a char* array is done like this:

char entries [number_of_items][lenght]  = { "entry1", "entry2", .... };

Apart from that, you can save yourself a lot of trouble by using an array of std::strings:

std::string entries[] = { "entry1", "entry2", ... };

Solution 3

You're entering one big string in your array. You need to separate the strings with commas.

Solution 4

Aside from the missing commas in your set of initializers, why not just skip making it a 2-dimensional array of char? You're only passing the array data to strstr(), and all it wants are null terminated strings. Why not use the more flexible:

char* array[] = { 
    "Becky Warre, 555-1223",
    "Joe Looney, 555-0097",
    "Geri Palmer, 555-8787",
    "Lynn Presnell, 555-1212",
    "Holly Gaddis, 555-8878",
    "Sam Wiggins, 555-0998",
    "Bob Kain, 555-8712",
    "Tim Haynes, 555-7676",
    "Warren Gaddis, 555-9037",
    "Jean James, 555-4939",
    "Ron Palmer, 555-2893",
    NULL
};

Now that the array is just a set of pointers (and it's terminated with a NULL pointer), the loop can look something like:

char *ptr = NULL;
int i;
for (i = 0; array[i] != NULL; i++)
{
        ptr = strstr(array[i], search);
        if (ptr != NULL)
                cout << array[i];
}

One advantage of this is that you can add new strings to the data without having to update any corresponding 'size' numbers. And you'll never have to worry about the compiler complaining that the array is too small in one dimension or another - it'll just make the array the size it needs to be.

Share:
16,268
Raptrex
Author by

Raptrex

Updated on June 11, 2022

Comments

  • Raptrex
    Raptrex almost 2 years

    I keep getting this error: initializer-string for array of chars is too long Even if I change num and length to 1, it still gets the error:

    #include <iostream>
    #include <cstring>
    using namespace std;
    
    int main()
    {
        const int num = 11;
        const int length = 25;
        char array[num][length] = { "Becky Warre, 555-1223"
                                    "Joe Looney, 555-0097"
                                    "Geri Palmer, 555-8787"
                                    "Lynn Presnell, 555-1212"
                                    "Holly Gaddis, 555-8878"
                                    "Sam Wiggins, 555-0998"
                                    "Bob Kain, 555-8712"
                                    "Tim Haynes, 555-7676"
                                    "Warren Gaddis, 555-9037"
                                    "Jean James, 555-4939"
                                    "Ron Palmer, 555-2893" };
    
        char search[length];
        cout << "Enter a string to search: ";
        cin.getline(search, length);
    
        char *ptr = NULL;
        int i;
        for (i = 0; i < num; i++)
        {
            ptr = strstr(array[num], search);
            if (ptr != NULL)
                cout << array[i];
        }
        if (ptr == NULL)
            cout << "No match found" << endl;
    
        return 0;
    }