Returning Memory from a Function
Another common use of dynamic memory involves returning pointers to memory blocks from a function. This is especially important when you're working with external libraries written by someone else.
Without this technique, the only things you can return from a function to the calling code are simple scalar values, such as an integer, a floating-point number, or a character. You cannot return more than one value or more complex data structures, like arrays.
This is why you need dynamic memory if you want to return something other than a simple value: you can allocate a block of memory in the function using malloc(), fill the block with something useful, and return the pointer to the block to the main part of the ...
Get C Programming: Visual Quickstart Guide now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.