You need to allocate memory for temp- at the moment, this is just dangling pointer. You can use mallocfor this, but note that the caller will need to ensure that this storage is subsequently freed.
For instance:
char *substring(const char *text, int position, int length)
{
char *temp = malloc(length + 1);
int i, j;
for (i = position, j = 0; i < position + length; i++, j++)
{
temp[j] = text[i];
}
temp[j] = '\0';
return temp;
}
source
share