C - Using pointer arguments to scanf_s

Description

Using pointer arguments to scanf_s

Demo

#define __STDC_WANT_LIB_EXT1__ 1
#include <stdio.h>

int main(void)
{
  int value = 0;/*from   w w  w .  j  a  v  a  2 s  . co  m*/
  int *pvalue = &value;                     // Set pointer to refer to value

  printf("Input an integer: ");
  scanf_s(" %d", pvalue);                   // Read into value via the pointer

  printf("You entered %d.\n", value);       // Output the value entered
  return 0;
}

Result

the scanf_s() statement:

scanf_s(" %d", pvalue);

We normally store the value entered by the user at the address of the variable.

We could have used &value, but the pvalue pointer is used to pass the address of value to scanf().

We already stored the address of value in pvalue when you created it:

int *pvalue = &value;                   // Set pointer to refer to value

pvalue and &value are the same, so you can use either.

printf("You entered %d\n", value);

Related Example