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

    Join Date
    Aug 2003
    Location
    UK
    Posts
    19
    Rep Power
    0

    strcmp - how can i pass char *mystring?


    Hi there,

    As the title suggests i am a newbie to C.

    I have a function called split which is designed to split two words into groups of characters, and compare the groups from each word together - the idea being a nice count of the number of substrings that match within the word (the size of the substrings being decided elsewhere). Whilst i am sure there are better ways of doing this, i have encountered a problem which I cannot solve.

    I want to compare the two substrings i have using strcmp. The code will hopefully better explain what i am trying to do.

    Code:
    char* split(char *toSplit, char *toSplit2)
    {
    	int innercount = 0;
    	for(int count = 0; count < wordlen; count++)
    	{
    		for(int loopcount = 0; loopcount < (gram); innercount++)
    		{		
    			//INNERLOOP - WORD TWO!
    			int innercount2 = 0;
    			for(int count2 = 0; count2 < wordlen2; count2++)
    			{
    				for(int loopcount2 = 0; loopcount2 < (gram); innercount2++)
    				{
    					if(strcmp(*(toSplit+innercount), *(toSplit2+innercount2)) == 0)
    					{
    						printf("%c (word1) == %c(word2) - matchingTokens++\n", (*toSplit+innercount), (*toSplit+innercount2));
    						matchingTokens += 1 ;
    					}
    					loopcount2++;
    				}
    				innercount2 -= (gram-1);
    			}
    			loopcount++;
    		}
    		innercount -= (gram-1);
    		printf("\n");
    	}
    	return toSplit;
    }
    Using this code i get a compilation error (VS 6). Which reads as:
    rror C2664: 'strcmp' : cannot convert parameter 1 from 'char' to 'const char *'
    Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast

    Can anyone please explain to me what i am doing wrong here please?

    I realise it is a stupid newbie pointer issue, but asking for help is the only way to learn!

    Cheers,

    aceduk
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Singapore
    Posts
    31
    Rep Power
    11
    this is just off my head not sure it works though...

    try:
    strcmp((const char*)(toSplit+innercount), (const char*) (toSplit2+innercount2))

    may not work coz i am answering this off my head, i did not test it

    truly sorrie if it doesnt...
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    UK
    Posts
    19
    Rep Power
    0
    LOL - i didn't even attempt to cast it as i thought i was doing something more seriously wrong! It compiles are runs fine now - the output isnt what i expected, however i will check everything else to make sure of what the problem is before questioning that neat little solution.

    Thanks Siddy!
  6. #4
  7. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    hi the problem is that you have a char * toSplit and you are adding to it int innercount. this is moving the pointer forward innercount bytes, then you are dereferencing that memory location and passing it to strcmp(). so you are passing what is actually at the memory location of (toSplit + innercount) which gets interpreted as an ASCII character . the correct way to call it would be
    Code:
     strcmp( (toSplit + innercount), (toSplit2 + innercount2) );
    Last edited by infamous41md; August 12th, 2003 at 02:21 PM.

IMN logo majestic logo threadwatch logo seochat tools logo