Passing Pointers By Reference

This is a discussion on Passing Pointers By Reference within the C Programming forums, part of the General Programming Boards category; I have to take this program and do the exact same effect bu instead of passing by value through the ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    53

    Passing Pointers By Reference

    I have to take this program and do the exact same effect bu instead of passing by value through the function discriminate I need to pass pointers by reference.

    Can anyone help?

    My AIM: awedaveo
    MSN: david_hern@hotmail.com
    __________________________________________________
    #include <stdio.h>
    #include <math.h>

    int discriminate(int a,int b,int c);

    int main()
    {
    int a,b,c,d;
    long double root1,root2;
    char choice[1];
    display_instructions();

    while(1)
    {

    printf("Do you want to continue? y/n\n");
    scanf("%s",&choice);

    if(choice[0]=='n')
    {
    break;
    }

    printf("Please enter a value for a: \n");
    scanf("%d",&a);
    printf("\nPlease enter a value for b: \n");
    scanf("%d",&b);
    printf("\nPlease enter a value for c: \n");
    scanf("%d",&c);

    d=discriminate(a,b,c);

    if(d>0)
    {
    root1=(-b+sqrt(d))/(2*a);
    root2=(-b-sqrt(d))/(2*a);
    printf("root1= %Lf\nroot2= %Lf\n",root1,root2);
    }
    else if(d==0)
    {
    root1=(-b+sqrt(d))/(2*a);

    printf("root1=root2= %Lf\n",root1);
    }
    else
    {
    printf("Based on your values there is no solution.\n");
    }

    }
    return 0;
    }

    int discriminate(int a,int b,int c)
    {
    int d;
    d=(b*b)-(4*a*c);

    return d;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void foo( int bar ); /* by value */
    void bar( int* foo ); /* by reference */
    
    ...
    
    int x;
    
    foo( x ); 
    bar( &x );
    There ya go.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    53
    Could you expand on it for this program?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by hern
    Could you expand on it for this program?
    *sigh* What don't you understand?
    Code:
    int discriminate(int a,int b,int c)
    Change that, to this:
    Code:
    int discriminate(int *a,int *b,int *c)
    Then dereference the pointers. Pass by address instead of by value as described. Give it a try. Post your changes and specifics to what you're not understanding or having problems with.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    53
    Here's what I got so far...Am I on the right track if not can you point me to a good direction..Thanks for your help..Bear with me I'm pretty new to pointers and C programming at that.

    #include <stdio.h>
    #include <math.h>

    int discriminate(int a,int b,int c);
    int get_discrim(int* a,int* b,int* c);

    int main()
    {
    int a,b,c,d;
    long double root1,root2;
    char choice[1];

    while(1)
    {

    printf("Do you want to continue? y/n\n");
    scanf("%s",&choice);

    if(choice[0]=='n')
    {
    break;
    }

    printf("Please enter a value for a: \n");
    scanf("%d",&a);
    printf("\nPlease enter a value for b: \n");
    scanf("%d",&b);
    printf("\nPlease enter a value for c: \n");
    scanf("%d",&c);

    d=get_discrim(&a,&b,&c);

    if(d>0)
    {
    root1=(-b+sqrt(d))/(2*a);
    root2=(-b-sqrt(d))/(2*a);
    printf("root1= %Lf\nroot2= %Lf\n",root1,root2);
    }
    else if(d==0)
    {
    root1=(-b+sqrt(d))/(2*a);

    printf("root1=root2= %Lf\n",root1);
    }
    else
    {
    printf("Based on your values there is no solution.\n");
    }

    }
    return 0;
    }

    int discriminate(int a,int b,int c)
    {
    int d;
    d=(b*b)-(4*a*c);

    return d;
    }
    int get_discrim(int* a,int* b,int* c)
    {

    int d;
    d=(b*b)-(4*a*c);

    return d;
    }

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You need to dereference your pointers in the actual function. Here, use this example to try and understand pointers, because you apparently don't.
    Code:
    #include <stdio.h>
    
    void foo( int *a )
    {
        *a += 5;
    }
    
    int main ( void )
    {
        int x = 0, y = 0;
    
        for( x = 0; x < 10; x ++ );
            printf("y is %d\n", foo( &y ) );
    
        return 0;
    }
    Enjoy.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Jun 2003
    Posts
    53
    So in order to get it to work I just need to fix what is in the get_discrim function or both the get_discrim and main functions?

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you prototype a function, the function itself has to be identical*. Thus:
    Code:
    int myfun( int *ptr );
    
    int main( void )
    {
        ... do stuff here ...
    
        return 0;
    }
    
    int myfun( int *ptr )
    {
        ... do stuff here ...
    }
    See? Both lines need to be the same.
    *Some exceptions do apply, but for now, just make them identical.

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Jun 2003
    Posts
    53
    Yes I know my question is within my actual get_discrim function I am writing I need to be depointering and using a reference to my variable d?

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quzah's Quick and Dirty Pointer Tutorial(TM)

    Pointers store addresses. Nothing more. Their 'value' that they contain is the address of another variable.
    Code:
    int x;    /* This is a variable. */
    int *ptr;    /* This is a pointer. */
    
    x = 5;    /* This is assigning a value to a variable. */
    ptr = &x;    /* This is assigning an address (a value) to a pointer. */
    
    *ptr = 10;    /* Assigning a value to x through the pointer. */
    Here, you dereference the pointer to get to whatever it points at. You are accessing 'x' through the pointer, by dereferencing it.

    You use & to get an address.
    You use * to dereference a pointer (to get access to whatever it points to).


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Jun 2003
    Posts
    53
    Yes I know how to do all of that..I've already made the function prototype ie: int get_discrim(int* f, int* g, int* h);

    I'm now defining the functiong:

    int get_discrim(int* f, int* g, int* h)
    {
    int d;
    d=(b*b)-(4*a*c);

    return d;

    }

    Ok I know that the definition is completely wrong..I know that since I have these pointers being referenced I have to have them assigned to something so that I can calculate the discriminate...Do I need to be returning addresses to those variables a,b,c,d in order to do so or am I in the wrong direction?

  12. #12
    Registered User
    Join Date
    Jun 2003
    Posts
    53
    I think I got it some one let me know if this seems right:


    #include <stdio.h>
    #include <math.h>

    //int discriminate(int a,int b,int c);
    void get_discrim(int* a,int* b,int* c);
    void print_discrim(a,b,c);

    int main()
    {


    int a,b,c,d;
    long double root1,root2;
    char choice[1];

    while(1)
    {

    printf("Do you want to continue? y/n\n");
    scanf("%s",&choice);

    if(choice[0]=='n')
    {
    break;
    }



    get_discrim(&a,&b,&c);
    print_discrim(a,b,c);

    if(d>0)
    {
    root1=(-b+sqrt(d))/(2*a);
    root2=(-b-sqrt(d))/(2*a);
    printf("root1= %Lf\nroot2= %Lf\n",root1,root2);
    }
    else if(d==0)
    {
    root1=(-b+sqrt(d))/(2*a);

    printf("root1=root2= %Lf\n",root1);
    }
    else
    {
    printf("Based on your values there is no solution.\n");
    }

    }
    return 0;
    }

    void print_discrim(int a,int b,int c)
    {
    int d;
    d=(b*b)-(4*a*c);


    }

    void get_discrim(int* a,int* b,int* c)
    {

    printf("Please enter a value for a: \n");
    scanf("%d",&a);
    printf("\nPlease enter a value for b: \n");
    scanf("%d",&b);
    printf("\nPlease enter a value for c: \n");
    scanf("%d",&c);
    }

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void get_discrim(int* a,int* b,int* c)
    {
    
        printf("Please enter a value for a: \n");
        scanf("%d",&a);
        printf("\nPlease enter a value for b: \n");
        scanf("%d",&b);
        printf("\nPlease enter a value for c: \n");
        scanf("%d",&c);
    }
    Close. Since you are passint pointers, and scanf actually takes pointers as arguments, you do not need to use the address of operator on them. Just use their name:
    Code:
    scanf( "%d", a );
    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Registered User
    Join Date
    Jun 2003
    Posts
    53
    Ah so that's it then!? Awesome!!! I'm so proud of myself

    Thanks for your help

    Last thing..I get an error saying D is initialized but not used do u know how to fix?

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by hern
    Last thing..I get an error saying D is initialized but not used do u know how to fix?
    Code:
    void print_discrim(int a,int b,int c)
    {
    int d;
    d=(b*b)-(4*a*c);
    
    
    }
    Well, you don't actually do anything with D... Basicly this function has no purpose.

    Quzah.
    Hope is the first step on the road to disappointment.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing by Reference. Simple question.
    By d3m105 in forum C Programming
    Replies: 6
    Last Post: 10-31-2007, 12:47 PM
  2. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  3. Passing an array as reference to a function?
    By Kylecito in forum C++ Programming
    Replies: 10
    Last Post: 03-11-2006, 01:25 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 05:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21