I have written a function to process a variadic input arguments like below -
std::string ReturnMessage(const char * format, ...)
{
va_list vl;
va_start(vl, format);
std::stringstream ss;
for(int i = 0; format[i] != '\0'; ++i)
{
switch(format[i])
{
case 's':
{
char *str = NULL;
str = va_arg(vl, char *);
if(*str)
{ //Getting memory access violation here
ss << str;
}
}
break;
}
}
val_end(vl);
return ss.str()
}
No when user assigns non string value(ReturnMessage("%s %s","Testing",100)) as string and at that str gets assigned to Bad Pointer and if(*str) statement throws Access Violation exception which is valid here. But How can I prevent invalid string to get added into stringstream??