Question

I have a program below

#include<stdio.h>

void int_frac(double a, long *int_part,double *frac_part)
{
  *int_part = (long int)(a);
  *frac_part = a - *int_part;
}
int main()
{
  long int* ip;
  double* fp;
  double i = 1.2345467;

  int_frac(i,&ip,&fp);
  printf("%f = %ld + %f",i,*ip,*fp);
  return 0;
}

I am using gcc compiler. This program gives an error:

expected long int* but argument is of type long int**

and

expected double* but argument is of type double**

Why this error comes up.

Was it helpful?

Solution

The compiler is telling you that you are passing long int** where it expects long int*. And that you are passing double** where it expects double*.

The compiler is correct. You declared ip to be long int* and then passed &ip which has type long int**. You made the same mistake with fp.

What you have done is declare two pointers, ip and fp, but not initialized the pointers. The fundamental problem is that you have declared pointers but have not allocated any variables. If int_frac is to be able to write to variables, those variables must be allocated somewhere. So, what you need to do is declare two values, and pass the addresses of those values to int_frac.

Your code should be:

int main()
{
    long int ip; // allocates a variable
    double fp; // as does this
    double i = 1.2345467;

    int_frac(i,&ip,&fp); // pass the addresses of ip and fp
    printf("%f = %ld + %f",i,ip,fp);
    return 0;
}

OTHER TIPS

The error which you received is the answer in my opinion as you are passing long int** when it expects long int* You need to declare ip as long int ip like this:

int main()
{
    long int ip;
    double fp;
    double i = 1.2345467;

    int_frac(i,&ip,&fp);
    printf("%f = %ld + %f",i,ip,fp);
    return 0;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top