Click to See Complete Forum and Search --> : Arrays(C++ question)


anushreeg
December 18th, 2001, 02:38 AM
this is my function

char* clsRouterStub::CreateReqMsg(char* strParamListss,char strDelimiterss[])
{
char *strReturn = new char[100];
char *token;
int n;
char strParamList[]="a,bb,ccc,dddd";
char strDelimiter[]=",";

token = strtok( strParamListss, strDelimiter );
while( token != NULL )
{
if (token != "")
{
if (n>0)
n += sprintf(strReturn + n, "\317%s", token);
else
n = sprintf(strReturn , "%s", token);
}
token = strtok( NULL, strDelimiter );
}
return strReturn;
}

when i try to call it in following way then it doesn't take the correct value
Invocation :
char *strShakeHandReq = objRouterStub.CreateReqMsg("a,b,ccc,ddd",",");

while if i pass char a[20] = "a,b,ccc,ddd";
char b[10] = ",";

char *strShakeHandReq = objRouterStub.CreateReqMsg(a,b);
then it works why?

anushreeg
December 18th, 2001, 03:02 AM
now it is been solved.

Green_Beret
December 18th, 2001, 03:02 AM
Because in the first case you are trying to pass a const char string but you haven't declared the function as such.

Regards,
The Beret.

NMTop40
December 18th, 2001, 04:19 AM
you've said it is a C++ question but you've coded it all in C with the exception of the line where you used new (instead of malloc)

const should be used (in both C and C++) whenever you are not going to modify the string.

Also use * rather than [] when possible. (More efficient and, in my opinion, clearer)

Also why have you passed in strDelimiterss as a parameter then used your local strDelimiters?

And your local strParamList? Is this only for testing?