Thread: Trying to Replicate These Two Program Examples Demonstrating Utility of Pointers

  1. #1
    Registered User
    Join Date
    Dec 2016
    Posts
    45

    Trying to Replicate These Two Program Examples Demonstrating Utility of Pointers

    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
    Last edited by potomac; 12-29-2016 at 04:14 AM.

  2. #2
    Old Took
    Join Date
    Nov 2016
    Location
    Londonistan
    Posts
    121
    The problem is you forgot an & in your scanf.

    Couple of style issues.

    main really ought to be int main. You are relying on an implicit return value which is poor form, state that main returns an int explicitly.
    In the decompose function the return statement is extraneous rubbish, it's unneeded.

  3. #3
    Registered User
    Join Date
    Dec 2016
    Posts
    45
    Whoops, I missed that scanf &.

    Also corrected those style error

    I still, however, get compiler errors for uninitialized variables f1 and i

    Trying to Replicate These Two Program Examples Demonstrating Utility of Pointers-ddd-jpg

  4. #4
    Old Took
    Join Date
    Nov 2016
    Location
    Londonistan
    Posts
    121
    Just initialise them to 0. It'll work as you intend and do nothing.
    Code:
    #include<stdio.h>
    
    
    void decompose(float x, int intpart, float fracpart);
    int main()
    {
        int i = 0;
        float f = 0, f1 = 0;
    
    
        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);
        return 0;
    }
    
    
    void decompose(float x, int intpart, float fracpart)
    {
        intpart = (int)x;
        fracpart = x - intpart;
    }
    Last edited by Hobbit; 12-29-2016 at 04:43 AM. Reason: added code

  5. #5
    Registered User
    Join Date
    Dec 2016
    Posts
    45
    Ah thanks that worked! Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with multi function utility program
    By codefan3 in forum Linux Programming
    Replies: 1
    Last Post: 06-08-2013, 11:17 PM
  2. How to 'replicate' an array of hex values?
    By sef0 in forum C Programming
    Replies: 8
    Last Post: 05-27-2009, 08:53 AM
  3. A C++ program examples showing Polymorphism, please help.
    By MarkSquall in forum C++ Programming
    Replies: 19
    Last Post: 06-06-2008, 04:41 AM
  4. Please Try out my Utility Program!!!
    By dgprog in forum C++ Programming
    Replies: 19
    Last Post: 07-20-2002, 10:40 PM
  5. how to call the utility sort from within a C program?
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 02-25-2002, 04:03 PM

Tags for this Thread