开发者

initializer-string for array of chars is too long

开发者 https://www.devze.com 2022-12-10 09:02 出处:网络
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:

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;
}


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" }


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", ... };


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号