Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

cannot convert parameter 1 from 'char' to 'const char *'

Status
Not open for further replies.

sntnkndg

Programmer
Jun 18, 2007
5
DE
Dear Experts,

I am stuck up with a piece of code, which I am not able to break. Could you kindly help me out.

Situation:
I am trying to perform a string comparison between 2 variables, one defined as const TCHAR * and other defined as CS_CHAR, inside an Embedded SQL program in C++. My relevant code snippet is as below:

=====
short CSQLBox::fnTest(const TCHAR *varInput)
{
CS_CHAR szExtract;

//szExtract variable is populated by Embedded SQL query
....

//Do string comparison
if(strcmp(szExtract, varInput)==0)
{
...
}
}
=====

When I try to compile this code, the strcmp step is throwing an error "cannot convert parameter 1 from 'char' to 'const char *'".

I have tried typecasting parameter 1 as:
if(strcmp((const char*)szExtract, varInput)==0)

This way, the code compiles, but fails in runtime with unhandled exception error. I am unable to understand how to solve this issue.

Could anyone kindly offer me some suggestions.

cheers,
sanK
 
szExtract is a char, not a null terminated string, which is what strcmp requires. You can compare the first char in varInput to szExtract, but that's all. To do what you want, you need to change the data type of szExtract to a null terminated char array, or compare the first char in varInput to szExtract.

Lee

 
Hello trollacious,

Thank you for your response. I must say I had tried to be too specific and had missed out to mention that szExtract is an array. The actual piece of code goes like this:

=====
short CSQLBox::fnTest(const TCHAR *varInput)
{
CS_CHAR szExtract[10];
szExtract[0] = _T('\0');

//szExtract array is populated by Embedded SQL query
....

//Do string comparison
for( int j=0; j<5; j++)
if(strcmp(szExtract[j], varInput)==0)
{
...
}
}
=====

And the same error message is shown during compilation. Could you kindly suggest.

cheers,
sanK
 
Please use [code][/code] tags when posting code.


--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
You're still trying to compare a single char to a null-terminated string. If the last char in the array is '\0', then you could try the following, though the rest of your code makes me doubtful that it will work the way you want.

Code:
if(strcmp(&szExtract[j], varInput)==0)

How about explaining in simple English what you're trying to accomplish with that code?

Lee
 
Looks like CS_CHAR is actually a char. strcmp works on null terminated strings. If you wish to compare for individual characters, either do them one at a time (1) or stick the character into a 2 byte string with the second byte being a null terminator (2). szExtract holds the whole string then just compare using szExtract (3)

Code:
// 1
   for ...
   {
      if (szExtract[j] == *varInput) ...
// 2
   for ...
   {
       char szCompare[2];
       szCompare[0] = szExtract[j];
       szCompare[1] = '\0';
       if (strcmp (szCompare, varInput) == 0)...
// 3
// remove the for loop
   if (strcmp (szExtract, varInput) == 0)...

Note that if you are using TCHAR, you should actually be using _tcscmp instead of strcmp. That way, if you #define UNICODE, you won't hit more problems.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top