我被要求制作一个代码,使用指针将3个输入的整数重新排列为升序/降序。
我需要使用函数order()
来返回指向函数ascending3()
或descending()
的指针,具体取决于' e'进入了。
我一直在readInts()函数中指定的行上出错,我不知道如何修复它。
错误
lvalue required as unary ‘&’ operand" -- The error for `ptr = &(order(e))`
warning: assignment makes pointer from integer without a cast -- The error for `ptr=order(e)`
指针代码
void readInts(){
int *a,*b,*c;
char e;
int (*ptr1)(int*,int*,int*);
int result;
printf("Enter Integer 1:");
scanf("%d", a);
printf("Enter Integer 2:");
scanf("%d", b);
printf("Enter Integer 3:");
scanf("%d", c);
printf("Enter either A or D:");
scanf(" %c", &e);
ptr1 = &(order(e)); /*ERROR HERE*/
result = (*ptr1)(a,b,c);
printf("%d %d %d", a, b, c);
}
功能
int ascending3(int* x, int* y, int* z)
{
/*removed sorting code for the sake of shortening the question*/
*x=smallest;
*y=middle;
*z=largest;
}
int descending(int* x, int* y, int* z)
{
int swap;
ascending3(x,y,z);
swap=*x;
*x=*z;
*z=swap;
}
int (*order(char e))(int*x ,int*y,int*z)
{
if(e=='a')
{
return ascending3;
}
else if(e =='d')
{
return descending;
}
return;
}