'Tim Lewis' twlewis@reagan.com [c-prog]
2017-12-23 19:57:33 UTC
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
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