Doubt Related to Pointers usage in the function parameters?
I have a two programs they consists of main and a function.
In the first case.
I am sending the output parameters to the function like below.
In the function i am putting the values into the output parameters and then i am displaying
them in the main.
Here i need to access the output parameters with the '*' in function.
Code:
void getSumDifAndProduct(int a,int b,int *arr);
void getSumDifAndProduct(int a,int b,int *arr)
{
*(arr+0) = a+b;
*(arr+1) = (a-b);
*(arr+2) = (a*b);
}
In the second case
Iam using the structure variable and p
Code:
typedef struct
{
int sum;
int diff;
int product;
}OutStruct_t;
void getSumDifAndProduct(int a,int b,OutStruct_t *pSt);
void getSumDifAndProduct(int a,int b,OutStruct_t *pSt)
{
pSt->sum = a+b;
pSt->diff = (a-b);
pSt->product = (a*b);
}
int main()
{
int a=0,b=0;
OutStruct_t *pSt;
printf("Please Enter the a and b values \n");
scanf("%d %d",&a,&b);
pSt = (OutStruct_t *) malloc(sizeof(OutStruct_t));
getSumDifAndProduct(a,b,pSt);
printf("a and b is %d %d \n",a,b);
printf("a and b sum is %d \n",pSt->sum);
printf("a and b dif is %d \n",pSt->diff);
printf("a and b product is %d \n",pSt->product);
return 0;
}
Here if i use *pSt->sum why does it gives me error like
ex3.c:16: error: invalid type argument of `unary *'
Iam bit confused about this variation.