Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

All floats in a single function with call by reference

Tags:

c

#include<stdio.h>

int main()
{
    int x;
    float peri ,area1;
    scanf("%d",&x);
    area(x, &peri, &area1);
    printf(" %f %f ", peri, area1);
    return 0;
}

int area(int r, float *per, float *are)
{
    *per = 2.0 * r;
    *are = 3.14 * r * r;
}

This works fine but when I try to change the input value to float

#include<stdio.h>

int main()
{
    float x, peri, area1;
    scanf("%f",&x);
    float area(x, &peri, &area1);
    printf(" %f %f ", peri, area1);
    return 0;
}

float area(float r, float *per, float *are)
{
    *per = 2.0 * r;
    *are = 3.14 * r * r;
}

I get:

error: expected ')' before '&' token

like image 532
Sudhanshu Gupta Avatar asked Feb 03 '26 12:02

Sudhanshu Gupta


2 Answers

 float area( x, &peri, &area1);

should be

area( x, &peri, &area1);

You don't need to specify the return type when you are calling the function.

Also since the return type now is non-int you need to either:

  1. Move the function definition before main.
  2. Provide a function declaration like:

    float area(float, float *, float *);
    

    before main.

Also since you are not returning anything from the function you should be making its return type as void.

like image 199
codaddict Avatar answered Feb 06 '26 04:02

codaddict


float area( x, &peri, &area1); 

declares a function, While what you are trying to do is to call the function so it should be:

area( x, &peri, &area1); 
like image 36
Alok Save Avatar answered Feb 06 '26 03:02

Alok Save