Reputation: 43
In a beginner program I am working on, I must use 3 functions to get the sum and difference of two arguments. On the third function, I have to store the result in two pointers. My issue is, I have not been able to successfully integrate the third function into main.
My main function is as follows:
int main ()
{
int num1, num2;
int sumResult, differenceResult;
printf("Please enter a number: ");
scanf("%d", &num1);
printf("Please enter another number: ");
scanf("%d", &num2);
sumResult = CalculateSum (num1, num2);
printf("the result of the sum is %d\n", sumResult);
differenceResult = CalculateDifference (num1, num2);
printf("the result of the difference is %d\n", differenceResult);
return 0;
}
My function that deals with pointers :
void CalculateBoth(int num1, int num2, int*sumPtr, int *diffPtr)
{
*sumPtr = num1+num2;
*diffPtr= num1-num2;
}
Any suggestions?
Upvotes: 2
Views: 340
Reputation: 58271
To understand clearly read answer linearly
First understand you function declaration(read comments):
void CalculateBoth(int num1, int num2, int*sumPtr, int *diffPtr);
// ^
// return type ^--------^ ^-----------^
// first two are simple int last two are pointer to integers
Notice return type of your function is void
means it doesn't return (or say return void).
But your function CalculateBoth()
is good It calculate both sum and difference.(two things)
In C programming we can't return more then one argument from a function. And in this case pointer are very useful. So last you arguments are actually output arguments of function CalculateBoth() sun and difference.
like(again read comments):
void CalculateBoth(int num1, int num2, int*sumPtr, int *diffPtr);
// ^
// return type ^--------^ ^-----------^
// two input arguments Output sum and difference
Because pass by value doesn't reflect change at calling statement we do use pass by address variable.
Pointers are variable that can hold address of other variable.
Because in main()
function you need both sum and difference of both variables num1
and num2
. we pass address of two result variables when we call function CalculateBoth()
from main()
.
So you result variables in main() are:
int sumResult, differenceResult;
you from main like:
CalculateBoth(num1, num2, &sumResult, &differenceResult);
Note: ampersand operator &
in front of variable sumResult
, differenceResult
is The address-of operator gives the address of its operand. By simple writing variable name you are passing value, but if you use &
operator you passes address of a variable.
function CalculateBoth()
having local copy of their variables. But in case of last two arguments function stores result at address instead of their local copy so result reflects in calling function main()
.
void CalculateBoth( int num1, int num2, int*sumPtr, int *diffPtr)
//variable types are: value value address address
{
*sumPtr = num1+num2; //store result of + memory pointer
*diffPtr= num1-num2; // by variable sumPtr, *diffPtr
}
Note: *
here is value at address operator gives value stored at an address. and The result of the operation is the value addressed by the operand; that is, the value at the address to which its operand points. So you are storing at memory pointer by two variables
So instead call your function:
differenceResult = CalculateBoth (num1, num2);
call like
CalculateBoth(num1, num2, &sumResult, &differenceResult);
additionally, you are doing wrong, if you writea differenceResult =
in front of above function calling expression because CalculateBoth()
returns nothing.
Upvotes: 1
Reputation: 66194
I think you're asking how to do this, which assumes your CalculateSum()
and CalculateDifference()
functions appear before this code. (you didn't provide them in the source listing):
void CalculateBoth(int num1, int num2, int *sumPtr, int *diffPtr)
{
*sumPtr = CalculateSum(num1, num2);
*diffPtr= CalculateDifference(num1, num2);
}
int main ()
{
int num1, num2;
int sumResult, differenceResult;
printf("Please enter a number: ");
scanf("%d", &num1);
printf("Please enter another number: ");
scanf("%d", &num2);
// invoke both sum an difference through a unified front-end
CalculateBoth(num1, num2, &sumResult, &differenceResult);
printf("the result of the sum is %d\n", sumResult);
printf("the result of the difference is %d\n", differenceResult);
return 0;
}
Upvotes: 1
Reputation: 13046
Pointers are used (usually) to pass not argument value but address in memory where argument value is located. This allows you to modify parameters value.
Let's take function that adds 2 to argument and passes result through result variable as an example:
void WrongAdd2(int argument, int result)
{
result = argument + 2;
}
In this case you pass result value but any change in result will be local. Pointer instead tells not about value but address where value is located.
void Add2(int argument, int *result)
{
*result = argument + 2;
}
Here result 'address of int' so *result is place in memory pointed by result.
To use pointers in functions you should have place in memory for result:
int value = 10;
int result = 0;
WrongAdd2(value, result); /* result is still 0. */
Add2(value, &result); /* result is updated and now it is 12. */
So you need to call CalculateBoth with the same first 2 arguments but results will be passed through 3rd and 4th arguments which are pointers to places (contain their addresses) where you want to place output parameters.
CalculateBoth(num1, num2, &sumResult, &differenceResult);
You are telling: well, take values of num1 and num2 and pass ADDRESS OF sumResult and differenceResult as output parameters.
Upvotes: 1
Reputation: 3366
I am not sure what do you mean by integrate. I think this is what you are looking for:
CalculateBoth(num1, num2, &sumResult, &differenceResult)
Upvotes: 0
Reputation: 34625
Call it like -
CalculateBoth(num1, num2, &sumResult, &differenceResult);
Not sure what you are misunderstanding here. For example -
*sumPtr = num1+num2;
Since sumPtr
is pointing to sumResult
, the result is acutally written to it. And you can print the calculated values in main once the function returns.
Upvotes: 0