August 12th, 2003, 06:08 AM
strcmp - how can i pass char *mystring?
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.
Using this code i get a compilation error (VS 6). Which reads as:
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 ;
innercount2 -= (gram-1);
innercount -= (gram-1);
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!
August 12th, 2003, 07:04 AM
this is just off my head not sure it works though...
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...
August 12th, 2003, 08:06 AM
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.
August 12th, 2003, 02:18 PM
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
strcmp( (toSplit + innercount), (toSplit2 + innercount2) );
Last edited by infamous41md; August 12th, 2003 at 02:21 PM.