i try to read a number from keyboard with a function, but it doesn't work. This is the code
#include <stdio.h>
#include <stdlib.h>
void write(int *n);
int main()
{
int *n,*m,p;
write(&n);
write(&m);
p = *n + *m;
printf("p = %d",p);
}
void write(int *n)
{
scanf("%d",&n);
}
applying all the comments to the posted code and following the axiom:
only one statement per line and (at most) one variable declaration per statement.
and checking the returned value from scanf()
to assure the operation was successful
results in:
#include <stdio.h> // scanf(), printf(), fprintf()
#include <stdlib.h> // exit(), EXIT_FAILURE
void getInteger(int *n);
int main( void )
{
int n;
int m;
int p;
getInteger(&n);
getInteger(&m);
p = n + m;
printf("p = %d",p);
}
void getInteger(int *n)
{
if( 1 != scanf("%d",n) )
{
fprintf( stderr, "scanf failed" );
exit( EXIT_FAILURE );
}
}
and when run with an input of: '1 3' the output is
p = 4
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments