Functions return pointers—memory locations—just like any other value. Within a function, you can allocate a chunk of memory, then return the memory's address as a pointer. Pointers returned from a function need not be declared static as the address is a single value, just like any single value returned from a function.
- [Instructor] Like any other data type, … a function can return an address. … In this exercise file, function allocate … is a character pointer function. … It returns the address of a character or a character buffer. … Within the function, a chunk … of memory is allocated to a specific size. … Now, if the malloc function fails, … the exit function quits the program … at line 12; otherwise, the address … of the allocated buffer is returned in line 14. … In the main function, two pointer variables … are declared, A and B. … Both are passed to the allocate function … to set aside 256 and then 512 bytes … of storage, respectively, build and run. … And, both buffers were successfully allocated. … If you worked with a C language, you might think … that the value returned from this function must … be declared static. … After all, won't it go away when the function terminates? … Won't the value of variable P be lost? … Well, no, because memory is allocated at line eight. … Now, that memory chunk exists and the address assigned …
- Working with arrays
- Building a structure
- Creating an array of structures
- Testing characters
- Working with strings in C
- Using pointers to manipulate data
- Manipulating files
- Using command-line arguments
- Working with time functions
Skill Level Intermediate
1. Arrays and Structures
2. Characters and Strings
4. Files and the Operating System
5. The Interesting and Extraordinary
Next steps1m 52s
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.