I'm continuing on with my textbook and going through the examples on pointers.
There's one example that compares two similar programs to illustrate the value of using pointers as arguments to avoid the problem of passing arguments by value.
I wanted to use the code fragments provided in the text to recreate the programs, and compare the results.
I got the modified example with pointers to work, but I couldn't get the original program to work (at least in the full form I created). I wanted to see how "i and f weren't affected by the assignments to intpart and fracpart, so they have the same values after the call as they did before the call." (See red text below)
The problem is that the compiler is complaining say I didn't initialize some of the variables.
I tried initializing them and running the program again, but that crashed the program. How can I modify the program to get the results I want?
See screenshots, code, and book text below:
Screenshots: http://i.cubeupload.com/ZMxBti.jpg
Code in Question --- the program that's giving me the compiler errors about the unitialized variables f, f1 and i:
Code:
#include<stdio.h>
void decompose(float x, int intpart, float fracpart);
main()
{
int i;
float f, f1;
printf("Input a floating point number: ");
scanf_s("%f", f);
decompose(f, i, f1);
printf("The integer part of %f is %d, and the fractional part is %f1\n", f, i, f1);
}
void decompose(float x, int intpart, float fracpart)
{
intpart = (int)x;
fracpart = x - intpart;
return;
}
Here was the code with the pointers that worked, by the way. Not important to my question:
Code:
#include<stdio.h>
void decompose(float x,int* intpart, float* fracpart);
main()
{
int i;
float f, f1;
printf("Input afloating point number: ");
scanf_s("%f",&f);
decompose(f, &i,&f1);
printf("The integerpart of %f is %d, and the fractional part is %f1\n", f, i, f1);
}
void decompose(float x,int* intpart, float* fracpart)
{
*intpart = (int)x;
*fracpart = x - *intpart;
return;
}
--------------------------------------------------------------------------
Original Text
(Page 166)
C's requirement that arguments be passed by value makes it difficult to write certain kinds of functions. For example, suppose that we need a function that decompose a float value into an integer part and fractional part. Since a function can't return two numbers, we might try passing a pair of variables to the function and having it modify them:
void decompose(float x, int intpart, float fracpart)
{
intpart = (int)x;
fracpart = x - intpart;
return;
}
Suppose that we call the function in the following way:
decompose(3.14159, i, f);
At the beginning of the call, 3.14159 is copied into x, i's value is copied into intpart, and f's value is copied into fracpart. The statements inside decompose then assign 3 to intpart, and 0.14159 to fracpart, and the function returns. Unfortunately, i and f weren't affected by the assignments to intpart and fracpart, so they have the same values after the call as they did before the call. With a little extra effort, decompose can be made to work by using pointers. (See section 11.4)
(Page 211)
We saw in section 9.3 that a variable supplied as an argument in a function cal is protected against chance, because C passes arguments by value... Pointers offer a solution to this problem... let's modify the decompose function.. because of the & operator in front of i and f, the arguments are pointers to i and f, not the values of i and f