c - invalid operands to binary * (have ‘int’ and ‘int *’) -
i trying find out factorial of number using recursion , passing pointers function arguments. error appears time. debugers , coders! need assistance this.
the code #include<stdio.h> int *factorial(int *); int value, p=0, q=1, x, tmp; void main() { int *result; puts("enter value::"); scanf("%d",&value); result = factorial(&value); printf("\nthe result is::%d\n", *result); } int *factorial(int *n) { if(*n == p || *n == q) { return(&q); } else { tmp = *n -1; *n *= (factorial(&tmp)); return(n); } } error: error: invalid operands binary * (have ‘int’ , ‘int *’) *n *= (factorial(&tmp));
this line :
*n *= (factorial(&tmp));
should be
*n *= *(factorial(&tmp));
however, careful implementation because recursive, uses pointers globals.
would standard implementation not work you?
int factorial(int n) { if(n==0) homecoming 1; else homecoming factorial(n-1)*n; }
with standard, need prompt user non-negative values only.
c pointers recursion factorial
No comments:
Post a Comment