-
Pointer Error
Dear All,
I am trying to generate a sequence like the one below:
f3=1!+0!
f4=2!+1!
f5=3!+2!
by definition 0!=1
Below is my code for this problem, I am getting an error - passing arg 1 of fact makes pointer from integer without a cast
Code:
#include<stdio.h>
int fact(int a[]);
int main()
{
int n,*a,i;
scanf("%d",&n);
for(i=3;i<=n;i++)
{
a[1]=a[2]=1;
a[i]= fact(a[i-1])+ fact(a[i-2]);
printf("\t %d",a[i]);
}
system("pause");
return 0;
}
int fact(int a[])
{
int i,factorial=1;
for (i=1;i<=a[];i++)
{
factorial*=i;
}
return factorial;
}
Kindly help me out in debugging this error, I have tried changing int a[] to *a but to no avail. Thank you for your help.
-
So which function do you want to be responsible for filling the array with factorials?
-
It is the fact function which accepts the arguments to compute factorials.
-
The fact function should be int fact(int a). You're passing a simple integer. Make the loop for (i=1;i<=a;i++)
In main, you must allocate space for the array. If you expect the user to input a 10, then the array will have to be a[11]. So you need to limit the maximum allowable number and perhaps malloc() memory for the number of elements +1, or define a fixed array and make sure the user does not exceed this.
-
Thanks nonoob, problem solved! I actually wanted the array a to be dynamic, so that the user can enter any value of n and get the result!