我试图找出使用递归的数字阶乘并将指针作为函数参数传递。但是此错误始终出现。调试器和编码器!我需要您的帮助。
The code
#include<stdio.h>
int *factorial(int *);
int value, p=0, q=1, x, tmp;
void main() {
int *result;
puts("Enter the 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);
}
}
The error:
error: invalid operands to binary * (have ‘int’ and ‘int *’)
*n *= (factorial(&tmp));
这行:
*n *= (factorial(&tmp));
应该
*n *= *(factorial(&tmp));
但是,请谨慎使用此实现,因为它是递归的,但使用指向全局变量的指针。
标准实现对您不起作用?
int factorial(int n)
{
if(n==0)
return 1;
else
return factorial(n-1)*n;
}
使用标准,您只需要提示用户输入非负值即可。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句