I'm working on my intro C course assignment and I'm tasked with the following...
While my knowledge is basic, I believe I understand the gists In main
//first and second double hold the scanf inputs
double first;
double second;
//unsure here - to reference c and d as parameters in the function, do I simply declare unfilled double variables here?
double *c;
double *d;
printf("Enter your first number\n");
scanf("%f\n", &first);
printf("Enter your second number\n");
scanf("%d\n", &second);
//call the function, first and second by value, &c / &d by reference - correct?
pointerIntro(first, second, &c, &d);
For the function...
float myFunction(double a, double b, double *c, double *d)
{
c = a/b;
d = a*b;
//printf statements
}
I apologize if the flow of this question is messy but its part of the process for me :P
So, for my formal questions 1. is it correct to initiate two double pointer variables (*c & *d) in main to be passed as reference in the function? 2. Am I right to call the function with the reference pointers by saying &c / &d? 3. Any other critiques of this questioning?
It is perfectly valid. You can initialize and pass any number of pointer variables with their reference.
This is also valid..when you pass the variable address, you should store it into a pointers
you have to do some changes in your code,
You can assign directly a/b and a*b
pointer variables *c & *d
Then you have to read double number with %lf
format argument.
#include <stdio.h>
#include <string.h>
void myFunction(double a, double b, double *c, double *d)
{
*c = a/b; //change
*d = a*b; //change
printf("%lf %lf",*c,*d);
return;
//printf statements
}
int main()
{
//first and second double hold the scanf inputs
double first;
double second;
//unsure here - to reference c and d as parameters in the function, do I simply declare unfilled double variables here?
double *c;
double *d;
printf("Enter your first number\n");
scanf("%lf", &first); //change
printf("Enter your second number\n");
scanf("%lf", &second); //change
//call the function, first and second by value, &c / &d by reference - correct?
myFunction(first, second, &c,&d);
}