I am trying to write a function will take char then produces char* which is allocated in heap. How can I do that ?
Note : below code is not work, can you help to fix
Ex:
char* foo ( char x, char * xc ) {
xc = realloc ( xc, 1 + strlen ( xc ) ) ;
strcat ( xc, x ) ;
return xc ;
}
p = heap variable
foo ( 'a', NULL ) ==> ------------
| 'a'| '\0'|
------------
foo ( 'b', p ) ===> --------------------
| 'a' | 'b' | '\0' |
--------------------
foo ( 'c', p ) ===> --------------------------
| 'a' | 'b' | 'c' | '\0' |
--------------------------
NULLis not a string, therefore yout must not callstrlenon it.