char *substring(char *string, int index, int length)
{
//int counter = length - index;
int counter = 0;
for(;string[index] != string[length];index++, counter++);
printf("\n%d\n", counter);
char *array = malloc(sizeof(char) * counter);
if(array != NULL)
{
///while(string[index] != string[length])
while(index != length)
{
array[index] = string[index];
index++;
array++;
}
}
else
puts("Dynamic allocations failed\n");
return array;
}
1 - I've commented out initializing counter with "length - index" because I didn't feel comfortable with it(I also kinda like one line for loops:) ). So, can I count on counter if I used it in this simpler way.
2 - My problem with this code is that It doesn't return anything. I try to printf the result but nothing is printed and when I assign the result of the function to a char *, I get an error saying that I cannot assign a void to char *. But how is it returning void?
3 - Can I write this function with pointer arithmetic and without any array indexing at all?!
4 - Can I mutate the char *array ?!. I'm asking this because I thought char * cannot be mutated, but I've read a code that correctly mutated a char *. Or is it that I'm confusing a regular char * and a string?
Note: I do not want to use string library functions