I am trying to make an array of modifiable length, i defined a function called "add" that should add a char to the last of the array, but this result in putchar and printf not working. Why is this happening? and how can i fix it?
the output should be "hix", and the output is apparently ""
#include <stdio.h>
typedef struct
{
char* ptr;
size_t len;
}
bytes;
void add(bytes text, char chr)
{
text.ptr[text.len++] = chr;
}
bytes parse(char text[])
{
size_t index = 0;
while (text[index]) ++index;
return (bytes) {text, index};
}
void print(bytes text)
{
for (size_t index = 0; index < text.len; ++index)
{
putchar(text.ptr[index]);
}
}
int main()
{
bytes str = parse("hi");
add(str, 'x'); // if i remove this line "print" works, but only prints "hi"
print(str);
return 0;
}