A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.gnu.org/software/libc/manual/html_node/Dynamic-String-Input.html below:

Dynamic String Input (The GNU C Library)

12.14.6 Dynamically Allocating String Conversions

A GNU extension to formatted input lets you safely read a string with no maximum size. Using this feature, you don’t supply a buffer; instead, scanf allocates a buffer big enough to hold the data and gives you its address. To use this feature, write ‘a’ as a flag character, as in ‘%as’ or ‘%a[0-9a-z]’.

The pointer argument you supply for where to store the input should have type char **. The scanf function allocates a buffer and stores its address in the word that the argument points to. You should free the buffer with free when you no longer need it.

Here is an example of using the ‘a’ flag with the ‘%[…]’ conversion specification to read a “variable assignment” of the form ‘variable = value’.

{
  char *variable, *value;

  if (2 > scanf ("%a[a-zA-Z0-9] = %a[^\n]\n",
		 &variable, &value))
    {
      invalid_input_error ();
      return 0;
    }

  …
}

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4