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
Post a Comment