From the course: Complete Guide to C Programming Foundations

Unlock the full course today

Join today to access over 24,800 courses taught by industry experts.

Chapter solution: Processing input

Chapter solution: Processing input - C Tutorial

From the course: Complete Guide to C Programming Foundations

Chapter solution: Processing input

(upbeat music) - [Instructor] Here's my definition for the get_input function. It's a character pointer function as it returns a string. The argument is an integer, which I call size in the function. Within the function, I will need two variables. Character pointer A serves as the input buffer. In integer X is initialized to zero. This variable will serve as an offset. I need the offset so as not to disturb the address stored in variable A. The first step is to allocate the storage, which is based on the size value passed. If the allocation fails, the null pointer is returned, which is tested for. An error message is output. Then I use the exit function to terminate the program. I could just return the null pointer here, which is how I would code such a function for my own use. But remember, the null isn't tested for within the main function, so the program should just quit. The next step is to read input. The fgets statement stores the input into the buffer referenced by pointer A…

Contents