Link to home
Start Free TrialLog in
Avatar of sudarshantk
sudarshantkFlag for India

asked on

strcmp question

strcmp(buff[i], " ")

error C2664: 'strcmp' : cannot convert parameter 1 from 'char' to 'const char *'

Comparing buff[i] with blank space. Could you please let me know how to correct this error.
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi,
Change like this
strcmp(buff[i], " \0")

Deepu
Avatar of _iskywalker_
_iskywalker_

what is buf? maybe you should use strncmp so you can compare really a string, if you just compare 2 chars use what jkr gave you, strcmp is for comparing more than 1 char (string compare).
>> strcmp(buff[i], " ")
>>
>> error C2664: 'strcmp' : cannot convert parameter 1 from 'char' to 'const char *'
>>
>> Comparing buff[i] with blank space. Could you please let me know how to correct this error.

jkr's solution is correct. There are alot of different ways to do it but they are beside the point... I'd just like to add a little explanation, for this error, that will hopefully help you avoid this kind of error in the future.

Strings are implemented, in C, as a pointer to the first element in the array. Array elements are stored contiguously beginning at the memory location specified by a pointer. For example,

char* buff = "This is a test";

When you say,

buff[3]

You're telling the compiler to get the array element from index 3. This is accomplished by adding 3 units to the pointer. So, if the array started at memory location 12 and you wanted index 3, you would say buff[3] and the compiler would add them together and get the array element from memory location 15. The expression buff[3] can be rewritten to, *(buff+3). Both expressions perform exactly the same. It follows that buff[3] is not an array but an array element. In your case, you're using a single dimensional char array named buff. buff is a pointer to the first element in the array. buff[3] is the 3rd element in the array. buff[i] is a character, not a pointer. The difference is subtle but important.

The prototype for strcmp is,

int  strcmp ( const char * string1, const char * string2 );

string1 and string2 are both pointers to any array of type char. When you say,
>> strcmp(buff[i], " ")

1) You're missing a semicolon, but that's probably just a typo.
2) The compiler is complaining that you're passing buff[i], which is a character, to a function that is expecting a pointer. This is a problem for many reasons. The central problem is that a character is not a valid memory location. If you attempt to work in an address space that has not been allocated to your program, you're asking for a memory access violation which will crash your program.

P.S.
As an interesting bit of trivia,
buff[1] == *(buff + 1)

because is doesn't matter what order you perform addition, you can also say,
*(1 + buff) == 1[buff]

Cheers!
Exceter
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That's inherently very dangerous because the resulting array isn't null terminated. To safely use that approach, you'd have to use strncmp. For example,

// Only compare the first character
strncmp( &buff[i], " ", 1 );

Cheers!
Exceter
On my way to work this morning, it occured to me that Paul's suggestion isn't dangerous at all because strcmp(&buff[i], " "); creates a substring of the original string buff starting at index i and the terminating null character from the original string is included. However, this may not produce the desired result, if the first character in &buff[i] is a space, assuming that sudarshantk is trying to compare individual characters.

Cheers!
Exceter
I should probably say that my previous post assumed index i occured prior to the terminating null character.