Standard C - Strings

Hello I was wondering if someone could give me a good example how to use realloc with strings in standard C, not C++.

I am reading lines from a file char by char. And would like to make an empty char string called row. Then when I read a new character I would like to add that character at the end of that string and reallocated the memory. Since I don't know how long the strings are I can't just make char row[10]; Also when I reach new row I would like to empty this string. Could someone give me a good example how to make this. Not the reading part I have that, just the string appending part. Thank you.

Good information on realloc can be found here: http://www.cplusplus.com/reference/cstdlib/realloc/

1
2
3
4
5
6
7
8
9
10
char *test[] = {"Hello", "world"};
printf("%s\n",test[0]);
//this prints Hello
char *abc[] = {};
abc[0] = "test";
printf("%s",abc[0]);
//this prints test
abc[0] = strcat(abc[0],"name");
printf("%s",abc[0]);
//not working should print testname 
Last edited on
Up
strcat wants string as argument how do I cast char 'a' to string "a"?
Topic archived. No new replies allowed.