Passing by value and printing in function
-
Hello, I have an assignment I am working on, I am stuck at a beggining point. Below is the code I am working with. This is pretty much what the professor gave us to start with, all I am trying to do right now is pass some values to the subroutine and then print them in the subroutine to confirm the values passed. We have to pass by value. I can print the values in the main fine, but then I always get zeroes in the subroutine. I have a lot more to do in the assignment, but have been stuck here. Any help would be greatly appreciated. Thanks, Paul unsigned long adddouble(unsigned long x, unsigned long y) { printf("%ld %ld \n", x, y); return(x+y); /* no interest at this time */ } main() { double d1, d2, *dp; unsigned long *longp1,*longp2, s; scanf("%lf %lf", &d1, &d2); /* get two input real numbers */ printf("%lf %lf \n", d1, d2); longp1 = (unsigned long *) &d1; longp2 = (unsigned long *) &d2; s = adddouble(*longp1,*longp2); }
-
Hello, I have an assignment I am working on, I am stuck at a beggining point. Below is the code I am working with. This is pretty much what the professor gave us to start with, all I am trying to do right now is pass some values to the subroutine and then print them in the subroutine to confirm the values passed. We have to pass by value. I can print the values in the main fine, but then I always get zeroes in the subroutine. I have a lot more to do in the assignment, but have been stuck here. Any help would be greatly appreciated. Thanks, Paul unsigned long adddouble(unsigned long x, unsigned long y) { printf("%ld %ld \n", x, y); return(x+y); /* no interest at this time */ } main() { double d1, d2, *dp; unsigned long *longp1,*longp2, s; scanf("%lf %lf", &d1, &d2); /* get two input real numbers */ printf("%lf %lf \n", d1, d2); longp1 = (unsigned long *) &d1; longp2 = (unsigned long *) &d2; s = adddouble(*longp1,*longp2); }
longp1 = (unsigned long *) &d1; longp2 = (unsigned long *) &d2; this is not good. what you're doing here is setting a pointer to a long to point to the place where a double lives. i don't really see why you need to use pointers for any of the longs. try it without. -c
Image tools: ThumbNailer, Bobber, TIFFAssembler
-
Hello, I have an assignment I am working on, I am stuck at a beggining point. Below is the code I am working with. This is pretty much what the professor gave us to start with, all I am trying to do right now is pass some values to the subroutine and then print them in the subroutine to confirm the values passed. We have to pass by value. I can print the values in the main fine, but then I always get zeroes in the subroutine. I have a lot more to do in the assignment, but have been stuck here. Any help would be greatly appreciated. Thanks, Paul unsigned long adddouble(unsigned long x, unsigned long y) { printf("%ld %ld \n", x, y); return(x+y); /* no interest at this time */ } main() { double d1, d2, *dp; unsigned long *longp1,*longp2, s; scanf("%lf %lf", &d1, &d2); /* get two input real numbers */ printf("%lf %lf \n", d1, d2); longp1 = (unsigned long *) &d1; longp2 = (unsigned long *) &d2; s = adddouble(*longp1,*longp2); }