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 - 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
-
-
-
-
-
-
-
-
-
-
Understanding pointers3m 23s
-
(Locked)
Using pointers to manipulate data2m 53s
-
(Locked)
Doing pointer math3m 10s
-
(Locked)
Comparing arrays and pointers5m 5s
-
(Locked)
Challenge: Output a string57s
-
(Locked)
Solution: Output a string2m 44s
-
(Locked)
Allocating storage3m 42s
-
(Locked)
Working with a pointer array4m
-
(Locked)
Using pointers in structures3m 8s
-
(Locked)
Challenge: Allocating structures1m 3s
-
(Locked)
Solution: Allocating structures3m 34s
-
(Locked)
Passing pointers to functions2m 23s
-
(Locked)
Returning pointers from a function2m 31s
-
Chapter challenge: Processing input1m 26s
-
(Locked)
Chapter solution: Processing input3m 14s
-
-
-
-