Thread: Pointers and Call-by-reference functions

  1. #16
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Changed "a" to pass by pointer

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    void program_intro(void);
    void input_read(float *a, float b, float c, float x_initial, float x_final, float x_delta);
    int main()
    {
        float a, b, c, x_initial, x_final, x_delta;
        program_intro();
        input_read(&a, b, c, x_initial, x_final, x_delta);
        printf("a = %2.f\n", a);
        printf("b = %2.f\n", b);
        printf("c = %2.f\n", c);
        printf("x = %2.f\n", x_initial);
        printf("x[i] = %2.f\n", x_final);
        printf("delta X = %2.f\n", x_delta);
        return 0;
    }
    void program_intro(void) //Explanation of program to the user
        {
            printf("This program will compute a range of values for\n");
            printf("the second degree polynomial f(x) = ax^2 + bx + c,\n");
            printf("the derivative f'(x) = 2ax + b, \n");
            printf("the integral F(x[i]) = (a/3)x[i]^3 + (b/2)x[i]^2 + cx, and the\n");
            printf("area A = F(x) - F(x[i]).\n\n");
            return;
        }
    void input_read(float *a, float b, float c, float x_initial, float x_final, float x_delta)
        {
            printf("Please input the polynomial coefficients as follows:\n\n");
            printf("Coefficient a: ");
            scanf("%f", a);
            printf("\nCoefficient b: ");
            scanf("%f", &b);
            printf("\nCoefficient c: ");
            scanf("%f", &c);
            printf("\n\nPlease input the range and increment to be tested as follows:\n\n");
            printf("x: ");
            scanf("%f", &x_initial);
            printf("\nx[i]: ");
            scanf("%f", &x_final);
            printf("\ndelta x: ");
            scanf("%f", &x_delta);
        return;
        }
    Edit: Add link Pointers in C - Tutorial - Cprogramming.com
    Please read the "Pointing to Something: Retrieving an Address" first example code till you understand it.


    Tim S.
    Last edited by stahta01; 01-11-2018 at 10:39 AM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  2. #17
    Registered User
    Join Date
    Nov 2017
    Posts
    29
    Quote Originally Posted by stahta01 View Post
    In C, you can only return at most a single variable value.

    Therefore this line will not work.
    Code:
    return a, b, c, x_initial, x_final, x_delta;
    This can be fixed by using pointers.

    Tim S.
    Would I modify the prototype and definition to be *a, *b,.. and the declaration and return be &a, &b,...? I'm thinking my printf's need the same &a treatment?

  3. #18
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by scotdani View Post
    Would I modify the prototype and definition to be *a, *b,.. and the declaration and return be &a, &b,...? I'm thinking my printf's need the same &a treatment?
    I did the changes for parameter "a" and posted that changed code.
    READ THE LINK I POSTED! Then ask an question that makes some sense.

    Edit: Link to the post that you seemed to have ignored Pointers and Call-by-reference functions

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #19
    Registered User
    Join Date
    Nov 2017
    Posts
    29
    Quote Originally Posted by stahta01 View Post
    I did the changes for parameter "a" and posted that changed code.
    READ THE LINK I POSTED! Then ask an question that makes some sense.

    Edit: Link to the post that you seemed to have ignored Pointers and Call-by-reference functions

    Tim S.
    Look, I'm not interested in being belittled for my novice experience with C, and it was a timing issue that I did not immediately see your edited code.

    That being said, thank you for demonstrating what the syntax looks like for pointers in this example. This is how I am learning the ways of C, something reading cannot match.

    A professor told me this once: "If you don't understand what's on page two, you're not going to understand page 8."

  5. #20
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    C doesn't have pass by reference, it only has pass by value (a pointer is a value) so the declaration would be as Tim provided and to call it use &var (which is a pointer to var so it will match what the function wants as an argument/s)

  6. #21
    Registered User
    Join Date
    Nov 2017
    Posts
    29
    Quote Originally Posted by Hodor View Post
    C doesn't have pass by reference, it only has pass by value (a pointer is a value) so the declaration would be as Tim provided and to call it use &var (which is a pointer to var so it will match what the function wants as an argument/s)
    Ok I'm starting to get it now. When using pointers in main, say if I took this function and place it in main, how different or similar would it look?

  7. #22
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I'm not sure what you mean by "if I took this function and place it in main". C doesn't really support functions inside functions. Did you mean call this function from main?

    Perhaps go back and check out post #2. That shows a clear example of the difference between passing in a thing and a pointer-to-a-thing, including how to call it from main.

    You're doing something similar, but with several parameters, and they're type double instead of int.

  8. #23
    Registered User
    Join Date
    Nov 2017
    Posts
    29
    Quote Originally Posted by anduril462 View Post
    I'm not sure what you mean by "if I took this function and place it in main". C doesn't really support functions inside functions. Did you mean call this function from main?

    Perhaps go back and check out post #2. That shows a clear example of the difference between passing in a thing and a pointer-to-a-thing, including how to call it from main.

    You're doing something similar, but with several parameters, and they're type double instead of int.
    What I was thinking was how to pass pointers in main only compared to between main and a function. One of the assignment constraints is to scan for the variables in main then use a function for each calculation using pointers to these variables. I have trouble visualizing it unless I write it myself since I'm not a programmer.

    Which brings me to the next step in the assignment. The functions mentioned above are to use an incremented loop from x[I] to x[f] at increment delta[x]. Assuming this is an array, say x_range[N], this needs to be a pointer as well for the functions to run through the stored values; would it be smart to make an array for storing each calculated value too? I'm a little rusty on for loop structure with arrays; below is an outline of what I think it might look like, but I'm very uncertain.

    Code:
    #define NMAX 100
    double base_poly_func(*x_range[N];)
    //
    //Previous code with scans
    //
    double x_range[NMAX];
    double base_poly_results[N];
    
    for (i=0; N<NMAX; N+x_delta) //loop for getting all values in data set
    {
    x_range[N] += x_initial + x_delta;
    }
    
    for (i=0; N<NMAX; N++)
    {
    base_poly_results[N] += base_poly_func(&x_range[N]);
    }
    
    //
    //more code to follow
    //
    double base_poly_func(*x_range[N])
    {
    double base_poly_eq;
    base_poly_eq = x;  //actual equation goes here
    return;
    }

  9. #24
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    For, While and Do While Loops in C - Cprogramming.com
    Functions in C - Cprogramming.com
    Quote from Arrays in C - Cprogramming.com
    The fact that arrays can act just like pointers can cause a great deal of confusion.
    Tim S.
    Last edited by stahta01; 01-13-2018 at 05:55 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  10. #25
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Modified code from Arrays in C - Cprogramming.com
    to show the normal way to pass an array to an function

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int CheckForA(char * array, int size);
    
    #define ARRAY_SIZE 10
    
    int main()
    {
        char astring[ARRAY_SIZE] = {0};
        
        /* Using scanf isn't really the best way to do this; we'll talk about that
           in the next tutorial, on strings */
        scanf( "%s", astring );
    
        if (CheckForA(astring, ARRAY_SIZE))
        {
            printf( "You entered an a!\n" );
        }
        return 0;
    }
    
    int CheckForA(char * array, int size)
    {
        int i = 0;
    
        for ( i = 0; i < size; ++i )
        {
            if ( array[i] == 'a' )
            {
                return 1; /* true */
            }
        }
        return 0; /* false */
    }
    Tim S.
    Last edited by stahta01; 01-13-2018 at 06:11 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  11. #26
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by scotdani View Post
    I have trouble visualizing it unless I write it myself since I'm not a programmer.
    I suggest you avoid taking classes the requires programming; since, you do *not* seem to want to become a programmer!

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  12. #27
    Registered User
    Join Date
    Nov 2017
    Posts
    29
    Quote Originally Posted by stahta01 View Post
    I suggest you avoid taking classes the requires programming; since, you do *not* seem to want to become a programmer!

    Tim S.
    I certainly agree if I had the choice in the curriculum. I'm just looking for help to get a good grade and attempt to understand it. I hold no shame in holding someones hand through the programming portions.

  13. #28
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    FYI: Your code in post #23 was filled with errors; I posted links and example code to help you fix your code in the two posts after it.
    Pointers and Call-by-reference functions

    I have no plans to fix your bad code; it is so bad that it is not likely anyone will fix it for you.
    Link to Homework Policy: https://cboard.cprogramming.com/announcement.php?a=39

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  14. #29
    Registered User
    Join Date
    Nov 2017
    Posts
    29
    Quote Originally Posted by stahta01 View Post
    it is so bad that it is not likely anyone will fix it for you.
    Thank you. I will spend my time elsewhere.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 11-11-2013, 06:53 PM
  2. functions and pointers (call by value)
    By bos1234 in forum C Programming
    Replies: 3
    Last Post: 02-14-2011, 06:49 AM
  3. Sending array's to functions by reference or pointers
    By homeyg in forum C++ Programming
    Replies: 16
    Last Post: 12-27-2004, 02:55 PM
  4. Call-by-value Vs. Call-by-reference
    By Wiz_Nil in forum C++ Programming
    Replies: 3
    Last Post: 02-20-2002, 09:06 AM
  5. call by reference and a call by value
    By IceCold in forum C Programming
    Replies: 4
    Last Post: 09-08-2001, 05:06 PM

Tags for this Thread