# 2D arrays, strcpy and strcmp Problems

• 08-22-2008
Windu102
2D arrays, strcpy and strcmp Problems
Hi again. Having quite a number of problems with topics in title.

My function is meant to do the following.

> strcmp textblock with wordlibrary.
> If the element of 'wordlibrary' isn't empty (hence the != 0 - i initialised 'wordlibrary[][]' = {0}), and if 'strcmp' says theres no match, then it 'strcpy' 'textblock' into the first element of 'wordlibrary'.
> If the word is repeated it just increase it's count.

Code:

``` char mytext[21] = {"my dog dog bill"}; char textblock[21] = {0}; char wordlibrary[10][21] = {0}; int frequency[10] = {0}; int wordplace(char textblock[], char wordlibrary[][], int frequency[]){ int f = 0; //f is for word library and counter while ((wordlibrary[f]<= 10) && (wordlibrary[f]!= 0)){         if((strcmp(textblock,wordlibrary)) == 1){               strcpy(wordlibrary[f], textblock);           frequency[f]++;                   f++;           }           if((strcmp(textblock, wordlibrary)) == 0){                   frequency[f]++;           } }           printf("%s\n %s", wordlibrary, frequency);   return 0; }```
The errors i get are :

1. 'wordlibrary' missing subscript
2. '<=' : no conversion from int to char
3. '<=' : char [1] differs in levles of indirection from int
4. 'strcmp': cannot convert parameter 2 from char [][1] to const char *
5. Same as 4.
6. end of file before the left brace '{' at....address.

Sorry to be such a fuss. I HAVE tried to repair this myself, but i can't fix what i don't fully understand.

Any advice/help would be much appreciated.

Thank You.
• 08-22-2008
matsp
First error:
Code:

`int wordplace(char textblock[], char wordlibrary[][], int frequency[]){`
In multidimensional arrays, you must supply the lower dimensions (all but the first set of [] MUST be filled, the first [] is optional (and ignored)).

I think you are missing a * or [] in your #2 error, as you are comparing a char array with an integer value (10). This is invalid - I'm not sure what you actually expected to have happen, so I can't recommend a solution.

#3 error would be same problem as #2 - comparing an integer with a char array.

#4 I'm pretty sure is related to #1.
#5 - Same as #4.

#6. You probably have misbalanced your braces for the blocks.

Also:
Code:

`        if((strcmp(textblock,wordlibrary)) == 1){`
I think you mean == 0.

--
Mats
• 08-23-2008
Windu102
Decided to go back and simplify things, so i got a better understanding of how things work. My question is, can i Loop the strcpy function?

Code:

```#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> int main(int argc, char *argv[]){ char wordlibrary[5][21] = {{"luke"},{"lukeson"},{"liar"},{"abstract"}, {"liar"}}; char wordbackup[5][21] = {{0},{0},{0},{0},{0}}; int f = 0; wordlibrary[f]; while (f <= 4){ printf("%s\n", wordlibrary[f]); f++; } /*f = 0; int g = 0; wordbackup(g); while ((f<=4) && (g<=4)){ strcpy(wordbackup[g], wordlibrary[f]); f++; g++; }*/ char onew[21] = {"system"}; char empty[21] = {0}; strcpy(empty, onew); printf("%s\n", empty); return 0; }```

I commented out my loop here, but when not commented out i get the following error.

Line 21. "Term does not evaluate to a function taking arguments