#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    UK
    Posts
    19
    Rep Power
    0

    newbie - probs passing argv to function and adding char to it


    Hi there,

    I am extremely new to C (coming from a java and basic background), this is day 2!

    I am writing a small program to split words into trigrams (groups of three letters). My main function handles user input of words. It passes each word to a function called "space". For the purposes of the trigram algorithm i require a space appended to the beginning and end of each word entered. I need "space" to return the newly appeneded space.

    The code is very much a work in progress where chunks are not implemented... however the "space" function does all it is needed to do - at least logically i think it does.

    When i compile the code i get an error (i am using VC++ 6.0 on winXP). The error is as follows:-

    d:\code\ngram\ngram.cpp(64) : error C2664: 'strcat' : cannot convert parameter 2 from 'char' to 'const char *'
    Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
    Error executing cl.exe.

    I assume this means that I have rushed through the explanation of pointers and i am passing the wrong thing to my "space" array, or something similar. Can anyone please point out what i have done wrong - and if possible a brief explanation as to why it is wrong / suggest what i would need to do? Any help is greatfully received - learning a new language without any help is a frustrating process!

    Thanks in advance,

    Nick Cox
    aceduk


    Here is my code :- (complete with typos and notes to self)
    Code:
    /* WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING		*
     *																																*
     *Currently the program will only cope with words LARGER than the NGRAMSIZE value, to correct this we need to look at changing	*
     *the number of spaces added to the start and end of words - ignoring grams entirely made up of ' ' characters.					*
     *																																*/
    
    #include <stdio.h>
    #include <string.h>
    
    #define NGRAMSIZE 3 /*change this for size of bigram (2) or trigam (3) etc*/
    
    int main(int argc, char *argv[]);
    int space(char word[]);
    
    int main(int argc, char *argv[])
    {
    	if(argc <= 1)
    	{
    		printf("Please run the program with the following arguement layout:\n");
    		printf("Ngram\tword1\tword2\n");
    		printf("Where word1 is the word you are comparing with word2 (the correct word)\n");
    		return -1;
    
    	}
    
    	if(argc == 2)
    	{
    		printf("You have only entered one word, if you wish to compare this word to another\nplease re-run the program.\n");
    		printf("Press 'y' if you wish to calculate the statistics for single word only.\n");
    		int c;
    		if ((c = getchar()) == 'y')
    		{
    			printf("Calculating trigram statistics for single word.\n");
    		}
    		else
    		{
    			return -1;
    		}
    	}
    
    	if(argc > 3)
    	{
    		printf("You have entered too many arguements.\n");
    		printf("Please run the program with the following arguement layout:\n");
    		printf("Ngram\tword1\tword2\n");
    		printf("Where word1 is the word you are comparing with word2 (the correct word)\n");
    		printf("\n\nExiting\n");
    		return -1;
    	}
    
    	space(argv[1]);
    	
    
    
    	return 0;
    }
    
    	int space(char word[])
    	{
    		char string[(sizeof(word[0])+2)]; //Change +2 to equal number of spaces added when code is re-written.
    		strcat( string, " "); //Place a space at the start of the string
    		for(int count = 0; count <= sizeof(word[0]); count++)
    		{
    			strcat( string, word[count]); //Add the word after the space
    		}
    		strcat( string, " "); //Place a space at the end of the string
    
    		return 1;
    	}
    	
    	/*void split()
    	{
    		int remaining = sizeof(word);
    		if (remaining > NGRAMSIZE)
    		{
    			//read first NGRAMSIZE characters
    			//delete first character from word
    		}
    		else if (remaining == NGRAMSIZE)
    		{
    			//read NGRAMSIZE character
    		}
    		else 
    		{
    			//finish and return
    		}
    			
    	}*/
    Last edited by aceduk; August 4th, 2003 at 09:01 AM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    First thought: you are in way over your head. There are so many issues you are not considering. As far as strcat() goes, you cannot use an uninitialized c-string as the destination. This code does not work:
    Code:
    int main()
    {
     char str[100];
     strcat(str, "Hello World");
     cout<<str<<endl;
    
     return 0;
    }
    str has to already contain a cstring to use strcat(). You can use strcpy() when there isn't a cstring in the destination array.

    Secondly, sizeof(word[0]) returns the number of bytes of the first letter of the char array word, which is 1. I think you want the length of what's contained in the word array, so you should use strlen(word), then add the two spaces you need. But, even that overlooks something: every c-string must end with a '\0', so you have to allow space for that. strlen() does not count the '\0', which means you need to add another space to arrive at the final size of your new array.

    One thing to consider is: why do you even need an array that is the exact size of what's in word plus the necessary spaces? Why not just declare an array new_word[100] and use that to build up your trigram? Or, if you must have the exact size array, since you know you are building up trigrams, you know the word array is going to be of length 3, so you can just add the 2 spaces you want in your function plus 1 space for the '\0' for a total length of 6. If you wanted your program to be more flexible and allow for quadgrams, quintgrams, etc., you could pass the x-gram size to the function along with the array, and then you will know the exact size for your array(=x + 2 + 1).

    Finally, as your code suggests, you don't realize that local variables are destroyed when a function ends. Since, you declare the array for your trigrams inside your function, when the function returns, that array will be destroyed, i.e. it won't exist anymore so you won't be able to use it.
    Last edited by 7stud; August 4th, 2003 at 01:34 PM.

IMN logo majestic logo threadwatch logo seochat tools logo