Converting Code to Function [difficulty]

I've been trying to convert some code into a function. The function would receive the user input stored in a pointer to char array allocated in dynamic memory, a pointer to an array of chars (possible inaccuracy of terms) for a list of words in an array, and an integer for the number of words in the array.

Here is the code

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
char * user_input = new char [10];
cin>> user_input;

int words_count = 100;         //100 words in the array
for(int A = 0 ; A<=words_count - 1; A++)
{ 
int w = 0;
  for(int B = 0 ; B<= strlen(words[A]) - 1; B++)
{ for(int C = 0 ; C<= strlen(user_input) - 1; C++)
{    
    if (words[A][B] == user_input[C])
         {    
                       // you can put cout 1 here to show all the times a letter matched               
             w++;      
                        // therefor at x = 0  w != 0 but w = 1   in the for loop
                       // after first instance of w+1 move on to next letter in 
                       // order to prevent w+1 on the same character
        }                             
    if (w == strlen(words[A]))
                      {
               
                      cout<< words[A] << endl;
                      break;
                      }
             } // end of C for loop
             } // end of B for loop
             } // end of A for loop

    
    
    delete [] words;
    delete [] user_input;


When I tried converting the code above to a function of its own the function looked like this

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
void find_words(char words[], int words_count, char user_input[])

{

for(int A = 0 ; A<=words_count - 1; A++)
{ 
int w = 0;
  for(int B = 0 ; B<= strlen(words[A]) - 1; B++)
{ for(int C = 0 ; C<= strlen(user_input) - 1; C++)
{    
    if (words[A][B] == user_input[C])
         {    
                       // you can put cout 1 here to show all the times a letter matched               
             w++;     // therefor at x = 0  w != 0 but w = 1   in the for loop
                      // after first instance of w+1 move on to next letter in order to prevent w+1 on the same character
        }                             
    if (w == strlen(words[A]))
                      {
               
                      cout<< words[A] << endl;
                      break;
                      }
             } // end of C for loop
             } // end of B for loop
             } // end of A for loop

    
    
    delete [] words;
    delete [] user_input;

}


But I kept getting compiler errors about not being able to convert type char to type const char and invalid types char[int] as subscripts. The functions compare each character in each string in the array with each character in the input by the user.
line 8, write words instead of words[A]

line 11, words is not a double dimension array, check it.

line 17, same problem as line 8.

line 29 & 30. they are not pointers. dont know what will happen if you delete them.
Thanks for your help writetonsharma, but I find your suggestions strange.

See, the code works fine in the main function, but when I try to make a function out of the code I get those errors. I used [A][B] to access a single character in an element (string). if words[0] was "dog", words[0][0] is 'd' .

words is defined as

char * words[3] = {"dog", "cat","horse"}; // for example
Last edited on
oh..ok thats what i was asking how words is defined.

Then there should not be any error in the code, all the above corrects are wrong.

to call your new function you can do this way:
void find_words(char **words, int words_count, char user_input[])

as words is a double pointer and you are passing it as a single dimensional array its giving error.
secondly you dont have to delete as you haven't done any new on it.
so remove line 29 and 30.



@writetonsharma

line 1 is dynamically allocated by new.

should I just delete line 29 then?

how do I pass words as a 2-dimensional array?
Last edited on
oh.. sorry i didnt see line 1. use delete on this then. whatever is new'd should be deleted.

i already told you, how to pass double dimension array:

void find_words(char **words, int words_count, char user_input[])
or this way:
void find_words(char words[][], int words_count, char user_input[])

first option will be better i think and you don't have to change anything in your function, just the signature of the function.
and yes, words is not new'd so remove line 29.
@writetonsharma

Thanks. Your first function prototype worked like a charm. Is char **words a pointer to a char pointer? Therefore, char **words takes the address of the pointer words?
char** words and char words[][] are the same. It doesn't matter which one you use.
take this piece of code as an example :

1
2
3
4
5
char value;
char*ptr1;
char **ptr2;
ptr1 = &value;
ptr2 = &ptr1;


now here ptr2 points to ptr1 and ptr1 points to value. its like a chain you see ptr2 stores the address of ptr1 while ptr1 stores the address of value so value an be accessed using ptr2 with the help of ptr1.
char ** words; is a pointer to pointer or a double dimensional array.
you got it correct.
Topic archived. No new replies allowed.