I have a pointer array named **lines, each line being 100 character long.
And have a function which adds characters to any given line and reallocates the line if it's not big enough:
void add_letter(char *line, int pos, char letter) {
if(strlen(line) % 100 == 0){
line = realloc(line, sizeof(char) * strlen(line) + 100);
}
...
add_letter
is called like this:
add_letter(lines[0], pos, ch);
line here is equal to lines[0]
, and in the second line, gdb returns 1
for p line == lines[0]
.
Here line
and lines[0]
are both in memory location 0x6465e0
.
But after the realloc
call, line
becomes 0x648200
, but lines[0]
stays in the same and becomes an ""
empty string.
What causes realloc
to act like this? Because of this reason, strcat(line, newline)
, newline
being the end result of addition doesn't work at the end of the function.