O'Reilly logo

C Programming: Visual Quickstart Guide by Marc Liyanage, Larry Ullman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required