c - I'm attempting a scanf inside a function and outputting it to another. currently its not outputting correctly. im not sure what I'm missing -


int read_number(int* value) {     scanf("%d",&value);     printf("%d\n",value); }   int main() {     int x=1;     read_number(x);     printf("%d",x);      return 0; } 

here's how code should be:

int read_number(int *value) {     scanf("%d", value);     printf("%d\n", *value); }   int main() {     int x=1;     read_number(&x);     printf("%d", x);      return 0; } 

you pass pointer parameter function, can dereference pointer , assign new value.

by way, it's best practice not use scanf() unsafe. should use fgets() , sscanf().


Comments

Popular posts from this blog

node.js - How to mock a third-party api calls in the backend -

node.js - Why do I get "SOCKS connection failed. Connection not allowed by ruleset" for some .onion sites? -

matlab - 0-by-1 sym - What do I need to change in order to get proper symbolic results? -