Hello,
I am writing a simple function which removes non numeric characters in a string. Here I use a char pointer by allocating memory to it. However, when I try to free the allocated memory, the program fails at runtime . Kindly let me know why this is happening.
Code:
char * FilterNumerics(char * Input, int iLength)
{
int i=0,j=0;
char *temp;
temp = (char *)malloc(iLength + 2);
if(!temp)
{
printf("Memory Allocation Failed\n");
return(1);
}
memset(temp,'\0',strlen(temp));
for (i = 0; i < iLength; i++)
{
if (Input[i] >= '0' && Input[i] <= '9')
{
temp[j] = Input[i];
j++;
}
}
free(temp);
return(0);
}
Also please let me know if there is a better way to achieve the above functionality.