1. ## Pointers

how do i get a pointer to "point" to a variable in another function?

this is what i have so far.

Code:
```# include <conio.h>
# include <stdio.h>

void calculations(int,int);

void main (int sum,int diff)
{
int num1,num2;
int * pointer1;
int * pointer2;

printf ("Enter num1: ");
scanf("%d",&num1);

printf ("Enter num2: ");
scanf("%d",&num2);

calculations(num1,num2);
pointer1 =&sum;
pointer2 =&diff;

printf ("The sum is : %d\n",pointer1);
printf ("The sum is : %d\n",pointer2);

getch();
}

void calculations (int num1,int num2)
{
int sum,diff;

sum = num1  + num2;
diff = num1 - num2;

}```

2. You don't. The variables sum and diff cease to exist when the function ends, and therefore you can't have pointers to them later (since there aren't any variables for them to point to). You can pass in pointers to a function, to have the function change the values those pointers point to; or you can have a function return a pointer (but not a pointer to a local variable!).

3. The parameter passing you have use doesn't make sense. e.g. main is always the first function executed, where do u suppose it would get sum & diff from? These variables would likely contain completely different values to what u expect (even if they were the right type).

Try this for calculations; void calculations (int *num1, int *num2, int *sum, int *diff). Note that you would have to declare sum & dif as int inside main, and pass the address of each variable to the function. Note that num1 & num2 are inputs and sum & diff are outputs.

4. so instead i shud just make the variables pointers and i'll be able to use em and get the correct value anywhere?

5. ok, say i have a function that does the basic arithmetic + - / *.

how do i get to use the results in that function without using global variables?

6. Originally Posted by Doc.
how do i get to use the results in that function without using global variables?
You can have the function return a value, or you can pass a pointer to the function and store the result in the object that is pointed to.

7. Originally Posted by laserlight
You can have the function return a value, or you can pass a pointer to the function and store the result in the object that is pointed to.

i know how to return a value but could you show me how to "you can pass a pointer to the function and store the result in the object that is pointed to."?

8. Originally Posted by Doc.
i know how to return a value but could you show me how to "you can pass a pointer to the function and store the result in the object that is pointed to."?
An example:
Code:
```#include <stdio.h>

void multiply(int* result, int x, int y);

int main()
{
int n;
multiply(&n, 4, 5);
printf("%d\n", n);
return 0;
}

void multiply(int* result, int x, int y)
{
*result = x * y;
}```

9. Or you can return a value like
Code:
```# include <conio.h>
# include <stdio.h>

calculations (int num1,int num2)
{
int sum ,diff;
char result;
fputs("For sum press s and for diff press anything else: ",stdout);
fflush(stdin);
scanf("%c",&result);
if(result=='s')
return sum = num1  + num2;
else
return diff = num1 - num2;
}
int main (void)
{
int num1,num2;
int lol;
fputs ("Enter num1: ",stdout);
scanf("%d",&num1);
printf ("Enter num2: ");
scanf("%d",&num2);
lol=calculations(num1,num2);
printf ("The diff is : %d",lol);
getchar();
return 0;
}```

10. @lolguy, Don't fflush(stdin) -- See the FAQ.

11. Originally Posted by lolguy
Or you can return a value like
If you actually read the thread, in particular post #7, you would know that your example is redundant. Besides what zacs7 mentioned, there are other areas in which you could improve:
• <conio.h> should not be included both because it is non-standard and because you use nothing from it.
• The calculations function should be declared with a return type of int instead of relying on the return type to default to int.
• The sum and diff local variables are redundant.
• The indentation is inconsistent.

12. Originally Posted by zacs7
@lolguy, Don't fflush(stdin) -- See the FAQ.

sometimes when i want a user to enter a string with " " (spaces) in it and i use gets, and sometimes it skips that input unless if i put a fflush infront of the gets.

why is that?

13. Originally Posted by Doc.
sometimes when i want a user to enter a string with " " (spaces) in it and i use gets, and sometimes it skips that input unless if i put a fflush infront of the gets.

why is that?
If you had previously used scanf to read in input, then there is still an enter-key in the input stream, which gets will read. The same FAQ has several methods for dealing.

14. Originally Posted by Doc.
sometimes when i want a user to enter a string with " " (spaces) in it and i use gets, and sometimes it skips that input unless if i put a fflush infront of the gets.

why is that?
Since fflush(stdin) results in undefined behaviour, it is possible that an implementation may actually cause it to "flush" the input buffer.

Oh, and don't use gets().

15. Yes, that's why I try to just stick to a couple ways of reading input, if in C, fscanf and if in C++ cin or getline.

Also, if you need extra help in understand pointers, try this C / C++ pointer tutorial it may help you understand them and remember them better.