I'm trying to write a fast function for filtering a char ** and putting the results into another char **
This is what I have written:
/*
-filterable is what is to be filtered
-filter is the filter
-filtered is the result applying the filter to filterable
*/
void filter(const char** filterable,const char * filter,char** filtered)
{
memset(filtered,'\0',sizeof(filtered));
int i=0;
int j=0;
int filter_length=strlen(filter);
int items=sizeof(filterable)/sizeof((char *) filterable); //segfault?
while(items--)
{
if((strncmp(filter,filterable[i],filter_length)==0))
strcpy(filtered[j++],filterable[i]);
i++;
}
}
The issue is I don't think I'm getting the number of rows from "filterable" properly as I'm getting a segfault. Any suggestions? Is this the fastest approach to filtering a char **?
Post-Comments:
Okay I read everyone's comments and the below seems to be working. Given this needs to be fast I threw in the register keyword although I've heard it doesn't guarantee anything these days.
void filter(char ** filterable, const char * filter, char ** filtered, int filters)
{
register int i=0;
register int j=0;
int filter_length=strlen(filter);
while(filters--)
{
if((strncmp(filter,filterable[i],filter_length)==0))
strcpy(filtered[j++],filterable[i]);
i++;
}
}