Name: Anonymous 2006-04-01 20:07
typedef char* String;
String getNth(String str, int n)
{
int i;
String temp = malloc(sizeof(char) * MAX_SIZE);
String regExp = malloc(sizeof(char) * MAX_SIZE);
for(i = 0; i < n; i++)
{
if (i == n-1) strcat(regExp, "%s");
else strcat(regExp, "%*s");
}
sscanf(str, regExp, temp);
free(regExp);
return temp;
}
this leaks memory (temp). a neat way to prevent this, please.
String getNth(String str, int n)
{
int i;
String temp = malloc(sizeof(char) * MAX_SIZE);
String regExp = malloc(sizeof(char) * MAX_SIZE);
for(i = 0; i < n; i++)
{
if (i == n-1) strcat(regExp, "%s");
else strcat(regExp, "%*s");
}
sscanf(str, regExp, temp);
free(regExp);
return temp;
}
this leaks memory (temp). a neat way to prevent this, please.