You can allocate memory before you call scanf()
. For example:
char moving[256];
if (scanf("%255s", moving) != 1)
…oops — presumably EOF…
You could use malloc()
instead of a simple array, but then you have to remember to free the allocated memory. OTOH, if you want to return the data from the function where it is read, it may well be more convenient to use malloc()
, but consider passing a pointer to the space (and its size?) to the function.
Or you can have scanf()
do the memory allocation for you (check the manual page for scanf()
carefully — read it weekly until you've memorized (enough of) it):
char *moving;
if (scanf("%255ms", &moving) != 1)
…oops — probably EOF, but perhaps OOM (out of memory)…
…use moving…
free(moving);
Yes, this is one of the lesser-known options in POSIX-standard scanf()
; it is not a part of Standard C.