Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C intro - How to pass a parameter by reference in function?

I'm working on my intro C course assignment and I'm tasked with the following...

  1. Write code for a function that receives two parameters (a,and b) by value and has two more parameters (c and d) by reference. All parameters are double.
  2. From main, use scanf to get two numbers, then call the function, and then display both returned values to the output in a printf statement.
  3. The function works by assigning (a/b) to c and assigning (a*b) to d.

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?

like image 615
Jon Gardocki Avatar asked Apr 13 '26 08:04

Jon Gardocki


1 Answers

Variables 'c' and 'd' don't have to be pointers to pass them by reference. So you have two cases:

  1. When you define 'c' and 'd' as pointers in main function you will pass them to function like this: pointerIntro(first, second, c, d); because they are already pointers and you don't need to send their reference, you just send them.

  2. If you define 'c' and 'd' just as double variables double c, d; you will send them to the function by reference using '&' symbol like this: pointerIntro(first, second, &c, &d);.

Then in your function to actually set the values of 'c' and 'd' you will need to dereference the pointer to them like this: *c = a/b; *d = a*b;.

If you are not familiar you can check what dereferencing the pointer means here: What does "dereferencing" a pointer mean?

Code that should work:

#include <stdio.h>

void myFunction(double a, double b, double *c, double *d)
{
    *c = a / b;
    *d = a * b;
}

int main(void)
{
    double a, b, c, d;

    scanf("%lf", &a);
    scanf("%lf", &b);

    myFunction(a, b, &c, &d);

    printf("%lf %lf", c, d);
}
like image 58
Harun Tucakovic Avatar answered Apr 14 '26 23:04

Harun Tucakovic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!