# Thread: Passing Pointers By Reference

1. ## 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. Code:
```void foo( int bar ); /* by value */
void bar( int* foo ); /* by reference */

...

int x;

foo( x );
bar( &x );```
There ya go.

Quzah.

3. Could you expand on it for this program?

4. 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.

5. 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. 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.

7. 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. 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.

9. 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. 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.

11. 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. 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. 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.

14. Ah so that's it then!? Awesome!!! I'm so proud of myself

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

15. 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.