I know how to make function with int, double, float with user input inside(im currently using scanf).
int getData(){
int a;
scanf("%i",&a);
return a;
}
but how to make function with string type and user input inside, then we return that value with type string?
A C string is an array of
char
terminated by a NUL (zero) byte. Arrays are normally passed around as pointers to the first element. The problem with returning that from the function is that the address pointed to must remain valid beyond the lifetime of the function, which means it needs to be either astatic
buffer (which is then overwritten by any subsequent calls to the same function, breaking earlier returned values) or allocated by the function, in which case the caller is responsible for freeing it.The
scanf
you mention is also problematic for reading interactive user input, e.g., it may leave the input in an unexpected state such as when you don't consume the newline at the end of a line the next call toscanf
(maybe in an unrelated function) may surprisingly fail to give the expected result when it encounters the newline.It is often simpler to read input into a buffer line-by-line, e.g., with
fgets
, and then parse the line from there. (Some inputs you may be able to parse without a buffer simply by reading character by character, but such code often gets long and hard to follow quickly.)An example of reading any string, which may contain whitespace other than the newline, would be something like:
Another option is to have the caller supply the buffer and its size, then just return the same buffer on success and
NULL
on failure. This approach has the advantage that the caller may decide when a buffer is reused and whether the string needs to be copied or simply read once and forgotten.Extending Arkku's approach to an unlimited size (in fact it is limited to SIZE_MAX - 1 characters) as input:
Call it like this: