2D arrays, strcpy and strcmp Problems

This is a discussion on 2D arrays, strcpy and strcmp Problems within the C Programming forums, part of the General Programming Boards category; Hi again. Having quite a number of problems with topics in title. My function is meant to do the following. ...

  1. #1
    Windu102
    Guest

    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.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Windu102
    Guest
    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


    Please and Thank You.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    > wordbackup(g);
    It's an array, and you're trying to call it as a function (hence the error message about something not being a function).

    Oh, and please fix the indentation before posting.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A Full Program to analyze.
    By sergioms in forum C Programming
    Replies: 2
    Last Post: 12-30-2008, 09:42 AM
  2. strcmp and strcpy error
    By behzad_shabani in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2008, 05:15 PM
  3. help with #define, strcmp, and strcpy
    By doc_watson2007 in forum C Programming
    Replies: 8
    Last Post: 12-20-2004, 10:50 PM
  4. Replies: 2
    Last Post: 02-23-2004, 06:34 AM
  5. strcpy & strcmp, what is wrong?
    By Silverdream in forum C Programming
    Replies: 7
    Last Post: 02-13-2002, 03:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21