Reputation: 125
Write a function called readRect() that uses scanf() to read the values defining the position of a rectangle. The values are integers in the order xmin, ymin, xmax, ymax. Your function should pass the parameters by reference to return these four integer values. Moreover, your function is to return an integer that is the number of values successfully scanned so the user can type a nonnumeric item such as eod to indicate no data (i.e., use the return type to return this value). I don't understand how I should create the function readRect() so that it passes the parameters by reference
int readRect (int w, int x, int y, int z){
return scanf("%d%d%d%d\n",&w,&x,&y,&z);
}
int main (void){
int a,b,c,d;
printf(">>enter two rectangles:\n");
readRect(a,b,c,d);
return EXIT_SUCCESS;
}
Upvotes: 0
Views: 93
Reputation: 106102
Rewrite it as
int readRect (int *w, int *x, int *y, int *z)
{
return scanf("%d%d%d%d",w,x,y,z);
}
and call it from manin
as
readRect(&a, &b, &c, &d);
Note that, in C, all the calls are by value and not by reference.
Upvotes: 1
Reputation: 8839
You need to pass the parameters by address, aka pointers. So, the function header will be:
int readRect ( int * w, int * x, int * y, int * z )
and the call from main
will be
readRect ( &a, &b, &c, &d );
Upvotes: 1