There are two things that need to be removed from the lesson when it is standing now:
(1) You should have one way to return a link to a new line, either as an argument passed by a reference to an OR function as a return value; you should not implement both.
(2) Since the routine that your teacher gave you allocates memory in a heap, it will be available for any part of your program, and you do not need to allocate any memory yourself. You should study the difference between heap memory, global memory and automatic memory (stack) so that you understand the differences between them and know how to work with each type.
(3) Since the memory is already allocated on the heap, there is no need to copy the line.
Given these facts, your code can be simplified something like this:
int main() { char *str = getstring(); printf( "\nString: %s", str ); return 0; } char* getstring(){ .... etc
Going forward, you want to think about how you allocate memory in your programs. For example, in this code a line is never highlighted. Itβs a good habit to think about your strategy to allocate your allocated memory.
source share