Discussion:
[c-prog] Cannot convert parameter from char to const char
'Tim Lewis' twlewis@reagan.com [c-prog]
2017-12-23 19:57:33 UTC
Permalink
I am trying to look through a string, and parse it based on a delimiter. It
works if I put in a hard-coded delimiter, but not when I try to use a
delimiter based on a char variable. I get the error:

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

I get what the error is saying; parameter 2 of strtok_s requires a constant.
But I have been unable to convert my char variable to a constant. I realize
this is probably a basic question, so I am very grateful for any assistance
or guidance.



#include "stdafx.h"

#include <iostream>

int _tmain(int argc, _TCHAR* argv[])

{

std::cout << "testing" << std::endl;

char parameterList[] = "/90/92/";

std::cout << parameterList << std::endl;



char *pszParameterList = parameterList;

char chDelimiter = pszParameterList[0];

std::cout << chDelimiter << std::endl;



char *context = NULL;

char *pchParameters = strtok_s(pszParameterList, chDelimiter,
&context); // This gives an error

// This works: char *pchParameters = strtok_s(pszParameterList, "/",
&context);

char *chParameterArray[20];

unsigned long ulParameterCount = 0;

while (pchParameters != NULL)

{

chParameterArray[ulParameterCount++] = pchParameters;

pchParameters = strtok_s(NULL, "/", &context);

}



return 0;

}





Tim
Thomas Hruska thruska@cubiclesoft.com [c-prog]
2017-12-23 21:30:32 UTC
Permalink
Post by 'Tim Lewis' ***@reagan.com [c-prog]
I am trying to look through a string, and parse it based on a delimiter. It
works if I put in a hard-coded delimiter, but not when I try to use a
cannot convert parameter 2 from 'char' to 'const char *'
Const isn't your issue here. The compiler will automatically handle a
cast to a const qualifier. The real issue is 'char' vs 'char *' (i.e.
the function expects a pointer).
--
Thomas Hruska
CubicleSoft President

I've got great, time saving software that you will find useful.

http://cubiclesoft.com/

And once you find my software useful:

http://cubiclesoft.com/donate/


------------------------------------

------------------------------------

To unsubscribe, send a blank message to <mailto:c-prog-***@yahoogroups.com>.
------------------------------------

Yahoo Groups Links

<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/c-prog/

<*> Your email settings:
Individual Email | Traditional

<*> To change settings online go to:
http://groups.yahoo.com/group/c-prog/join
(Yahoo! ID required)

<*> To change settings via email:
c-prog-***@yahoogroups.com
c-prog-***@yahoogroups.com

<*> To unsubscribe from this group, send an email to:
c-prog-***@yahoogroups.com

<*> Your use of Yahoo Groups is subject to:
https://info.yahoo.com/legal/us/yahoo/utos/terms/
John Matthews jm5678@gmail.com [c-prog]
2017-12-23 21:44:58 UTC
Permalink
Post by Thomas Hruska ***@cubiclesoft.com [c-prog]
Post by 'Tim Lewis' ***@reagan.com [c-prog]
I am trying to look through a string, and parse it based on a delimiter. It
works if I put in a hard-coded delimiter, but not when I try to use a
cannot convert parameter 2 from 'char' to 'const char *'
Const isn't your issue here. The compiler will automatically handle a
cast to a const qualifier. The real issue is 'char' vs 'char *' (i.e.
the function expects a pointer).
....and it needs to be a pointer to a string - you can't just replace
chDelimiter with &chDelimeter. That is, the function expects a string
of delimter characters, so the delimiter characters must be followed
by the end-of-sting character, '\0''.

You could do (untested):

char delimiters[] = {pszParameterList[0], '\0'};

and replace chDelimiter with delimiters (no &) in the strtok_s() call.
'Tim Lewis' twlewis@reagan.com [c-prog]
2017-12-23 23:11:05 UTC
Permalink
Thanks Thomas and John. The line that John sent worked perfect.



Tim



.....and it needs to be a pointer to a string - you can't just replace
chDelimiter with &chDelimeter. That is, the function expects a string
of delimter characters, so the delimiter characters must be followed
by the end-of-sting character, '\0''.

You could do (untested):

char delimiters[] = {pszParameterList[0], '\0'};

and replace chDelimiter with delimiters (no &) in the strtok_s() call.

__._,_
Pankaj Kumar pankaj_kum@yahoo.com [c-prog]
2017-12-24 10:57:54 UTC
Permalink
Dear Tim,Mere adding a typecasting line solved your problem....:-..................... // char *pchParameters = strtok_s(pszParameterList,&chDelimiter, &context); // This gives an error    char *pchParameters = strtok_s(pszParameterList,reinterpret_cast<char*>(&chDelimiter), &context); // solved..Pankaj(24/12/17)

I teste with rest of your code too...Hope this will fulfill your requirement..-Pankaj 
Paul Herring pauljherring@gmail.com [c-prog]
2017-12-24 15:33:09 UTC
Permalink
Post by Pankaj Kumar ***@yahoo.com [c-prog]
Dear Tim,
Mere adding a typecasting line solved your problem....:-
....................
// char *pchParameters = strtok_s(pszParameterList,&chDelimiter,
&context); // This gives an error
char *pchParameters = strtok_s(pszParameterList,
reinterpret_cast<char*>(&chDelimiter), &context); //
solved..Pankaj(24/12/17)
That's undefined behaviour, because you're passing a single character where
a null-terminated array of char is expected.
Post by Pankaj Kumar ***@yahoo.com [c-prog]
I teste with rest of your code too...
Of course, one symptom of undefined behaviour is the code running as
'expected'...
Post by Pankaj Kumar ***@yahoo.com [c-prog]
Hope this will fulfill your requirement..
-Pankaj
--
PJH
'Tim Lewis' twlewis@reagan.com [c-prog]
2017-12-25 18:06:43 UTC
Permalink
I need a little guidance on passing a variable to a .find. I have been reading and attempting to understand the requirement needed for .find, but I am not quite understanding. I am sure that this must be a basic theory in C++. I took a class in C and studied pointers, but that was 15+ years ago J

I am grateful for the guidance as I soak this in. One note: my code will be used to find invalid hex value. I am testing it with printable hex value to get it working.

Here is my code and issue:



//Find the hex value

char cDataIn[] = "test~data is here";

char *pchDataBuffer = cDataIn;

std::string sDataBuffer(pchDataBuffer);



char cHexValue[] = "\\x7E"; //hex tilde

char *pchHexValue = cHexValue;



std::cout<<"full hexc is |"<<cHexValue<<"|"<<std::endl;

std::cout<<"full hexcp is |"<<pchHexValue<<"|"<<std::endl;

// This works and returns position 4

std::string::size_type hexLocation1 = sDataBuffer.find("\x7E");

std::cout<<"Location is "<<hexLocation1<<std::endl;

// This gives incorrect strange value of 4294967295

std::string::size_type hexLocation2 = sDataBuffer.find(pchHexValue);

std::cout<<"Location is "<<hexLocation2<<std::endl;



In the second find, is it returning the memory location?



Tim
andrew clarke mail@ozzmosis.com [c-prog]
2017-12-25 22:05:40 UTC
Permalink
I need a little guidance on passing a variable to a .find. I have been
reading and attempting to understand the requirement needed for .find, but
I am not quite understanding. I am sure that this must be a basic theory
in C++. I took a class in C and studied pointers, but that was 15+ years
ago J
I am grateful for the guidance as I soak this in. One note: my code will
be used to find invalid hex value. I am testing it with printable hex
value to get it working.
//Find the hex value
char cDataIn[] = "test~data is here";
char *pchDataBuffer = cDataIn;
std::string sDataBuffer(pchDataBuffer);
char cHexValue[] = "\\x7E"; //hex tilde
char *pchHexValue = cHexValue;
std::cout<<"full hexc is |"<<cHexValue<<"|"<<std::endl;
std::cout<<"full hexcp is |"<<pchHexValue<<"|"<<std::endl;
// This works and returns position 4
std::string::size_type hexLocation1 = sDataBuffer.find("\x7E");
std::cout<<"Location is "<<hexLocation1<<std::endl;
// This gives incorrect strange value of 4294967295
std::string::size_type hexLocation2 = sDataBuffer.find(pchHexValue);
std::cout<<"Location is "<<hexLocation2<<std::endl;
You are searching for "\\x7E" vs "\x7E".

Note that string::find() will search for single characters, not just
substrings:

string::size_type loc = str.find('\x7e');

should be faster than:

string::size_type loc = str.find("\x7e");
In the second find, is it returning the memory location?
No. It's returning std::string::npos, as documented.
twlewis@reagan.com [c-prog]
2017-12-26 23:46:49 UTC
Permalink
Thanks Andrew, this worked perfect:

string::size_type loc = str.find('\x7e');

Tim
Pankaj Kumar pankaj_kum@yahoo.com [c-prog]
2017-12-26 17:17:26 UTC
Permalink
andrew is right.This line will solve the purpose as string.find will expect char, not a string...char c = '\x7E';std::string::size_type hexLocation2 = sDataBuffer.find(c);std::cout<<"Location is "<<hexLocation2<<std::endl;//will give you 4, as literal, you saw above
Loading...