-3

I want to call a function that calculates the width of a circle.

#include <stdio.h>
void area(double*);
int main()
{
    void* vp = NULL;
    double r;
    printf("input value : ");
    scanf(" %lf", &r);
    (double*)vp = &r;
    void area(vp);
}
void area(double* dp)
{
    double result;
    result = (*dp) * (*dp) * (3.14);
    printf("circle are is : %.2lf", result);
    return 1;
}*

I want to call a function in the //void area (vp)//, but I can not catch the error in visual stdio and proceed as it is. Do you know what the cause is?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
주경진
  • 3
  • 1

3 Answers3

2

To call the function, you write area(vp); not void area(vp);.

The latter, as it appears in the function main, is a function prototype and has no run-time effect.

And fix that definition of PI: yours is terribly inadequate given that you use a double type. See Math constant PI value in C

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • Is it a function declaration? In some ways, it is closest to that, but `vp` is not a type name so I think it is just a hybrid mess. The `void` prevents it from being a function call; the `vp` prevents it from being a function declaration. – Jonathan Leffler Dec 04 '17 at 15:02
0

You should write

  area(&r);

instead of the wrong and superfluous

 void area(vp);

Adding to the obvious mistake you made in the syntax

  • I don't see any reason for vp to be there.

  • to ensure success for scanf(), always check for the return value of the call.

  • Quoting C11, chapter §6.8.6.4,

    A return statement with an expression shall not appear in a function whose return type is void. [....]

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
0

There are three problems with the code.

For starters it is unclear why the pointer vp is declared as having the type void * though in the program it is used only to point objects of the type double.

Why not to declare it like

double *vp = NULL;

As result this trick with the pointer

(double*)vp = &r;

will not compile because in the left side the expression (double*)vp is rvalue and may not be assigned.

You could just write

vp = &r;

The second problem is that this statement

void area(vp);

is also invalid. To call the function you should write

area(vp);

And the third problem is that a function that has the return type void shall not specify an expression in the return statement

This statement shall be removed.

return 1;

From the C Standard (6.8.6.4 The return statement)

1 A return statement with an expression shall not appear in a function whose return type is void. A return statement without an expression shall only appear in a function whose return type is void.

There is no sense to declare the parameter of the function as having a referenced type.

The function definition could look like

double area( double dp )
{
    const double PI = 3.14;

    return dp * dp * PI;
}

The statement with the call pf printf should be moved in main

printf( "circle are is : %.2lf", area( r ) );
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335