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.
&) 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 thatscanfcall too).Piis already a pointer and&Piis a pointer to the pointer, a double poiner. So the call should beCalcPi(Pi, iterations);