7 Replies - 387 Views - Last Post: 25 August 2018 - 04:06 AM Rate Topic: -----

#1 lse123   User is offline

  • D.I.C Head

Reputation: -4
  • View blog
  • Posts: 75
  • Joined: 26-May 08

C - I get 0.0 for output ... error using pointers where is error

Posted 24 August 2018 - 09:20 AM

I get 0.0 for output(C language) ... error using pointers where is error - I am begineer in C and having dificulty in Pointers
can you debug???

#include <stdio.h>
#define PI 3.142 /* PI is a (symbolic) constant */


void sphere_calc(double radius, double *, double *);

double radius;

int main () {
    double v = 0.0;
    double s = 0.0;    
    
    printf("Enter a sphere radius: ");
    scanf("%f", &radius);

    double * pv = &v, * ps = &s;  
    sphere_calc(radius, pv, ps);

    printf("The surface is: %.3f and the volume is %.3f .", ps, pv);

    return 0;
}

void sphere_calc(double radius, double * v, double * s)
{
    *v = (4.0 / 3) * (PI * radius * radius * radius);
    *s = 4 * (PI * radius * radius);

    return;
}





Is This A Good Question/Topic? 0
  • +

Replies To: C - I get 0.0 for output ... error using pointers where is error

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14352
  • View blog
  • Posts: 57,554
  • Joined: 12-June 08

Re: C - I get 0.0 for output ... error using pointers where is error

Posted 24 August 2018 - 09:29 AM

Please copy/paste the entire error message here.
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6404
  • View blog
  • Posts: 21,972
  • Joined: 05-May 12

Re: C - I get 0.0 for output ... error using pointers where is error

Posted 24 August 2018 - 10:25 AM

The problem is that the printf()'s format specifiers are expecting to see doubles as arguments, but the code is passing in pointers to double.
Was This Post Helpful? 1
  • +
  • -

#4 lse123   User is offline

  • D.I.C Head

Reputation: -4
  • View blog
  • Posts: 75
  • Joined: 26-May 08

Re: C - I get 0.0 for output ... error using pointers where is error

Posted 24 August 2018 - 12:00 PM

View Postmodi123_1, on 24 August 2018 - 10:29 AM, said:

Please copy/paste the entire error message here.


It is a logic error
Compiles OK
Run OK

but getting

The surface is: 0.0000 and the volume is 0.0000

rather values... other than zero

View PostSkydiver, on 24 August 2018 - 11:25 AM, said:

The problem is that the printf()'s format specifiers are expecting to see doubles as arguments, but the code is passing in pointers to double.


// you mean run like this:
printf("The surface is: %.3f and the volume is %.3f .", s, v);

????

This post has been edited by lse123: 24 August 2018 - 12:24 PM

Was This Post Helpful? 0
  • +
  • -

#5 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6404
  • View blog
  • Posts: 21,972
  • Joined: 05-May 12

Re: C - I get 0.0 for output ... error using pointers where is error

Posted 24 August 2018 - 12:50 PM

Yes.

If you are using a modern compiler with the warning settings turned up, you should have gotten warning regarding the mismatch in parameter types.
Was This Post Helpful? 2
  • +
  • -

#6 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2201
  • View blog
  • Posts: 4,275
  • Joined: 30-May 10

Re: C - I get 0.0 for output ... error using pointers where is error

Posted 25 August 2018 - 02:19 AM

This is what Skydiver means.

$ gcc -Wall foo.c
foo.c: In function ‘main’:
foo.c:14:11: warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘double *’ [-Wformat=]
     scanf("%f", &radius);
           ^
foo.c:19:12: warning: format ‘%f’ expects argument of type ‘double’, but argument 2 has type ‘double *’ [-Wformat=]
     printf("The surface is: %.3f and the volume is %.3f .", ps, pv);
            ^
foo.c:19:12: warning: format ‘%f’ expects argument of type ‘double’, but argument 3 has type ‘double *’ [-Wformat=]
$ 



If you don't have a compiler which will diagnose printf/scanf format errors, then upgrade to one that will.
Was This Post Helpful? 2
  • +
  • -

#7 lse123   User is offline

  • D.I.C Head

Reputation: -4
  • View blog
  • Posts: 75
  • Joined: 26-May 08

Re: C - I get 0.0 for output ... error using pointers where is error

Posted 25 August 2018 - 02:41 AM

Thank you for your help... This is the correct script with corrections you say AND IS WORKING !!!!

#include <stdio.h>
#define PI 3.142 /* PI is a (symbolic) constant */


void sphere_calc(double radius, double *, double *);

float radius;

int main () {
    double v = 0.0;
    double s = 0.0;    
    
    printf("Enter a sphere radius: ");
    scanf("%f", &radius);

    double * pv = &v, * ps = &s;  
    sphere_calc(radius, pv, ps);

    printf("The surface is: %.3f and the volume is %.3f .", s, v);

    return 0;
}

void sphere_calc(double radius, double * v, double * s)
{
    *v = (4.0 / 3) * (PI * radius * radius * radius);
    *s = 4 * (PI * radius * radius);

    return;
}



Was This Post Helpful? 0
  • +
  • -

#8 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6404
  • View blog
  • Posts: 21,972
  • Joined: 05-May 12

Re: C - I get 0.0 for output ... error using pointers where is error

Posted 25 August 2018 - 04:06 AM

In C and C++, source code is called "code", not "script".
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1