typedef struct element element;
struct element{
dado_t str;
elemento* preview;
elemento* next;
};
typedef struct lista2 lista2;
struct lista2{
elemento* primeiro;
elemento* ultimo;
elemento* corrente;
};
void caller(lista2* l){
char *s = l->corrente->str;
char *s2 = l->corrente->next->str;
my_func(&s, &s2);
}
void my_func(char **s, char**s2){
size_t len = strlen(*s);
size_t len2 = strlen(*s2);
char *w = *s;
char *tmp = realloc(w, len + len2 + 1); //PROBLEM HERE
if(tmp != NULL)
*s = tmp;
else
*s = NULL;
strcat(*s, *s2);
}
When I run my code(before realloc()):
*w = "I Like Coffe"with memory adress:0x605050*s = "I Like Coffe"with memory adress:0x605050l->corrente->str = "I Like Coffe"with memory adress:0x605050
All good so far.
Now status after realloc(Before the assign *s = tmp):
*w = ""with memory adress:0x605050*s = ""with memory adress:0x605050l->corrente->str = ""with memory adress:0x605050
Still ok, right ? Now what I get after *s = tmp:
*w = ""with memory adress:0x605050*s = "I Like Coffe"with memory adress:0x605160CHANGEDl->corrente->str = ""with memory adress:0x605050
What I need:
1) Change l->corrente->str value in my_func();
2) Or somehow, change *s value to the new value after strcat. And keep l->corrente->str the same.
*sNULLand then dostrcat(*s ...)why? the whole point is to prevent undefined behavior which you are invoking, rather you shouldfree(*s). Also, I don't get your question, where in your code is*s = "I Like Coffe"? may be you need some Coffe? ;)*s = l->corrente->str. Andl->corrente->str= "I Like Coffe". I just think there is no need to show another function.realloc()with"I Like Coffe"'s address then nothing good will happen, but you can't be sure what in fact it will be.realloc()setting pointer to empty for a variant of this code.