2

First off, I have already looked at the example presented here:

Passing dynamically allocated array as a parameter in C

I am trying to pass a dynamically allocated array as a parameter to another function.

void InputIterations(int *iterations);
void CalcE(long double *E, int iterations);

int main()
{
     int iterations;  
     long double *E;

     InputIterations(&iterations);

     E = (long double *) malloc(iterations * sizeof(long double));

     CalcE(&E, iterations);
}

void InputIterations(int *iterations)
{
     printf("Enter a number of iterations: ");
     scanf("%d", iterations);
}

void CalcE(long double *E, int iterations)
{
     long double sum = 0;
     int i;
     for(i=0; i<iterations; i++)
     {
          sum = sum + /*formula for calculating irrational constant e*/
          *E = sum;
          E++;
     }
 }

However, when I compile my code I get the following error:

error: cannot convert ‘long double**’ to ‘long double*’ for argument ‘1’ to ‘void CalcE(long double*, int)’ CalcE( &E, iterations );

Does anyone knows why I am getting this error ?

If you could please explain my mistake or point me to a source that explains it I would greatly appreciate the help.

5
  • 6
    Take a look at the error message again. It really tells you all you need to know. And if you still don't know, then go back to a book and read more about the address-of operator (&) and what it does. You need to do it anyway, because you really misuse it in more than one place (for example I suggest you take a closer look at that scanf call too). Commented Oct 5, 2016 at 8:15
  • You need to know that Pi is already a pointer and &Pi is a pointer to the pointer, a double poiner. So the call should be CalcPi(Pi, iterations); Commented Oct 5, 2016 at 8:17
  • Choose a language. In C++ the answer is simply, "use std::vector". Commented Oct 5, 2016 at 8:27
  • Okay, I chose a language for you by removing the C++ tag. Commented Oct 5, 2016 at 8:28
  • 1
    If you use malloc, you should look into free() too. Commented Oct 5, 2016 at 8:30

2 Answers 2

3

Here:

CalcE(&E, iterations);

you take address of E (of type long double *) and pass it as an argument to CalcE. CalcE accepts as first parameter a pointer to long double. But when you take an address of E you are given actually a pointer to pointer to long double (long double**), and that is not a pointer to long double. And this is what your error tells you:

error: cannot convert ‘long double**’ to ‘long double*’ for argument ‘1’ to ‘void CalcE(long double*, int)’ CalcE( &E, iterations );

So you should have:

CalcE(E, iterations);
Sign up to request clarification or add additional context in comments.

Comments

3
CalcE(&E, iterations);

should be

CalcE(E, iterations);

Hope I helped

2 Comments

Yes this worked. Thank you! Is it because E was already a pointer so I did not need to pass the address of the pointer?
@Corey Yes, in passing the address, you're trying to pass a double pointer where a pointer is expected, hence the error, error: cannot convert ‘long double**’ to ‘long double*’ and also your program leaks memory because you never free the memory that E is pointing to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.