Find multiple char sequences in char array.

Hello im trying to make a program to find names inside char array, but i get 4 errors:
error: cannot convert 'char (*)[9]' to 'char (*)[]' in assignment (line 20)
error: cannot convert 'char (*)[8]' to 'char (*)[]' in assignment (line 21)
error: cannot convert 'char (*)[8]' to 'char (*)[]' in assignment (line 22)
error: invalid use of array with unspecified bounds (line 24)

Here is my code so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>

int main()
{
    char a[] = "Laurence";
    char b[] = "Iosefka";
    char c[] = "Gilbert";

    char NameList[] = "Willem,Arianna,Gilbert,Eileen,Laurence,Iosefka";

    char (*ptr)[];

    unsigned int KeyNumb = 0;
    unsigned int CorrectInRow = 0;
    unsigned int NameNumber = 0;
    ptr = &a;

    for(unsigned int i = 0; i < sizeof(ptr); i++){

        if(NameNumber == 0){ptr = &a};
        if(NameNumber == 1){ptr = &b};
        if(NameNumber == 2){ptr = &c};

        if(NameList[i] == *ptr[KeyNumb]){
            CorrectInRow++;
            KeyNumb++;
            if(CorrectInRow == sizeof(ptr)-1){
                std::cout << "Found: " << ptr << " At: " << i << std::endl;
                NameNumber++;
                KeyNumb = 0;
                CorrectInRow = 0;
                continue;
            }
        }
        else{
            KeyNumb = 0;
            CorrectInRow = 0;
        }
    }
    return 0;
}


Thank you for reading.
Last edited on
You missed your first error message that is spawned by line 16.

You are learning arrays, pointers, and C-strings, are you?

Replace line 11 with:
char *ptr {nullptr};
(and several points on later lines to be consistent with that.)
Yes kaskiverto im learning arrays, pointers, and C-strings.
I should probably white at the beginning.
So now i will have to assign like this?
1
2
3
...
ptr{&a};
...


how to compare this?
1
2
3
...
if(NameList[i] == *ptr[KeyNumb])
...


Thank you for your answer.
you can compare for equality of 2 c-strings via strcmp. if(strcmp(a,b) == 0) ..they are equal.

cstrings... you can do almost everything you need to do with just a few functions.

strcpy
strcmp
strstr

If you understand pointers, those 3 are almost all you need for most programs.
Last edited on
jonnin cant use strcmp() because it will only return first match and i'm planing on maybe having multiple of the same name.
Array decays to pointer trivially.

No, strcmp is not appropriate for a different reason, but strncmp is fine:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <cstring>

int main()
{
    const char* keys[] { "Laurence", "John", "Iosefka", "n", "Gilbert", "rt,E" };
    char NameList[] = "Willem,Arianna,Gilbert,Eileen,Laurence,Iosefka";

    for ( auto key : keys ) {
        std::cout << '=' << key << "=\n";
        size_t len = strlen( key ); // C-string is null-terminated
        const char* pos = NameList; // array decays to pointer
        while ( *pos ) { // C-string is null-terminated
            if ( strncmp( key, pos, len ) == 0 ) {
                std::cout << "Found " << key << " at "
                << (pos - NameList) << '\n'; // pointer math
            }
            ++pos;
        }
    }
}
repeated searching for same string multiple times?

char * c = yourstring;
do
{
c = strstr(c, "something");
if(c)
{count++; c++;} //count++ can be replaced with whatever when you found a match logic.
//c++ can be replaced with c+= strlen if you don't want nested matches.
}
while(c);
Last edited on
Thank you all for your answers i'm currently studying them, will mark as answered when finished.
Topic archived. No new replies allowed.