memory problem with string manipulation
I'm terrible when it comes to string manipulation, I always seem to find a way to segfaut or something. I'm trying to return the string between 2 offsets of another string but I'm getting weird results for what should be a simple function.
Code:
char* substr( char *searchstr, int offset1, int offset2 ){
int size = offset2-offset1;
char tmpstr[size];
int m = 0;
for( int i = offset1; i < size; i++ ){
tmpstr[m] = searchstr[i];
m++;
}
char *str;
str = (char*)malloc( sizeof( char ) * size );
strcpy( str, tmpstr );
return str;
}
Everything seems to work perfectly, however when I tried substr("wonderful", 0, 5) the returned result was "wonde +B" which leads me to believe I'm doing something wrong with the memory.