Thread: Pointer Error

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    69

    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.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So which function do you want to be responsible for filling the array with factorials?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    It is the fact function which accepts the arguments to compute factorials.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    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.

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    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!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer Error
    By xaykogeki in forum C Programming
    Replies: 10
    Last Post: 04-17-2010, 05:10 PM
  2. Replies: 4
    Last Post: 10-31-2009, 07:18 PM
  3. Pointer error.
    By george_1988 in forum C Programming
    Replies: 9
    Last Post: 09-06-2008, 12:47 AM
  4. Pointer error
    By Gaming in forum C++ Programming
    Replies: 8
    Last Post: 05-03-2008, 03:55 AM
  5. Pointer error
    By swgh in forum C++ Programming
    Replies: 6
    Last Post: 09-11-2006, 11:13 AM

Tags for this Thread