课程: Complete Guide to C Programming Foundations

今天就学习课程吧!

今天就开通帐号,24,100 门业界名师课程任您挑!

Returning pointers from a function

Returning pointers from a function

- [Instructor] When a function returns an address or memory location, the function's data type is a pointer. In this exercise file function, allocate is declared as a character pointer, meaning it returns the address of a character buffer. The function allocates memory to the integer size that's passed. When the allocation fails, the exit function quits the program. Otherwise, the allocated buffers address is returned. As a single value, the address need not be static. In the main function, two pointer variables are declared, A and B. Both are assigned allocated memory returned from the allocate function, buffers of 256 and 512 bytes respectively. Both buffers are freed before the program exits. Run. Both buffers are successfully allocated. The malloc function's argument is really a size T data type, which is the data type used to measure memory. So you could change the declaration from int to size T here. The compiler doesn't throw any errors when the variable is an integer, but this…

内容