I have to create a function that returns a string of characters from this one.
In parameter of the function there is (string, start index, len)
When I compile:
warning: address of stack memory associated with local variable 'ptr_s' returned [-Wreturn-stack-address]
I know that I have to allocate memory but I have restrictions I have to do it without.
I share my code with you:
char *new_str(char const *s, unsigned int start, size_t len)
{
char new[len];
size_t i;
i = 0;
if (!s || len <= 0 || start >= strlen(s))
return ("");
while (i < len)
{
new[i] = s[start];
i++;
start++;
}
new[i] = '\0';
return (new);
}
int main(void)
{
char const a[] = "bonjour les amis, ca va ?";
printf("%s", new_str(a, 0, 7));
return (0);
}