The way you call realloc is wrong. You should use a temporary variable to save the pointer because if realloc fails you will loose the reference the original memory block.
char *example(char* input);
int main(void)
{
char *input = malloc(sizeof(*input));
char *tmp;
if((tmp == example(input))) input = tmp;
printf("%s", input);
}
char *example(char* input) {
int i = 0;
char* a = "qweqeqweqweqweqweqwasdfsdfsdgasdgg";
for (int i = 0; i < 20; i++)
{
char *tmp = realloc(input, sizeof(*input) * (i + 2));
if(tmp)
{
input = tmp;
input[i] = a[i];
input[i + 1] = 0;
}
else
{
/* handle allocation error */
}
}
return tmp;
}
You can also use pointer to pointer but you need to save the original pointer to avoid potential memory leak:
int main(void)
{
char *input = malloc(sizeof(*input));
char *tmp = input;
example(&tmp);
if(tmp) input = tmp;
printf("%s", input);
}
void example(char** input) {
int i = 0;
char* a = "qweqeqweqweqweqweqwasdfsdfsdgasdgg";
for (int i = 0; i < 20; i++)
{
*input = realloc(*input, sizeof(*input) * (i + 2));
if(*input)
{
*input = tmp;
(*input)[i] = a[i];
(*input)[i + 1] = 0;
}
else
{
/* handle allocation error */
}
}
}
Another problem in your code: you do not null terminate your string.
You need to read warnings. You have more issues in your code - for example, you call function without prototype.