简体   繁体   English

返回指向函数错误的指针

[英]Return Pointer to Function Errors

I have been asked to make a code that will rearrange 3 entered integers into ascending/descending order using pointers. 我被要求编写一个代码,该代码将使用指针将3个输入的整数重新排列为升/降序。

I need to use the function order() to return a pointer to either function ascending3() or descending() , depending on what value of 'e' is entered. 我需要使用函数order()返回指向函数ascending3()descending()的指针,具体取决于输入的'e'值。

I keep getting an error on the line specified in the readInts() function and am not sure how to fix it. 我在readInts()函数中指定的行上不断收到错误,并且不确定如何解决它。

ERRORS 错误

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)`

Pointer code 指针代码

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);
    }

Functions 职能

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;
}

A function cannot return a function. 函数不能返回函数。 For this reason you cannot apply the address of operator ( & ) to the result of your function (to retrieve an address). 因此,您不能将运算符( & )的address of应用于函数的结果(以检索地址)。 But a function can return a pointer to a function. 但是函数可以返回指向函数的指针。

The name of a function in C (prefixed or not by the & operator) is always set as the address of the function, that's a pointer to that function. C语言中的函数名称(由&运算符添加或未添加前缀)始终设置为该函数的地址,即该函数的指针。

The correct code is: 正确的代码是:

int ascending3(int *x, int *y, int *z);
int descending(int *x, int *y, int *z);

typedef int (*fn)(int *x, int *y, int *z);

fn order(char e)
{

    if (e == 'a')
    {
        return ascending3;
    }
    else if (e == 'd')
    {
        return descending;
    }
    return NULL;
}

void readInts(void)
{

    int *a, *b, *c;
    char e;
    fn ptr1;
    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);
    result = (*ptr1) (a, b, c);

    printf("%p %p %p", a, b, c);
}

Where I used a typedef to declare the type of our function pointer (and also 2 prototypes for the ordering functions). 我在哪里使用typedef声明函数指针的类型(还有2个用于排序函数的原型)。

If you like to have the asterisk to better show the pointer nature of our type, you can define fn as function (not a pointer to function): 如果您希望使用星号更好地显示我们类型的指针性质,则可以将fn定义为函数(而不是指向函数的指针):

typedef int (fn)(int *x, int *y, int *z);

So you can use the asterisk notation: 因此,您可以使用星号表示法:

typedef int fn(int *x, int *y, int *z);

fn *order(char e)
{

    if (e == 'a')
    {
        return ascending3;
    }
    else if (e == 'd')
    {
        return descending;
    }
    return NULL;
}

void readInts(void)
{

    int *a, *b, *c;
    char e;
    fn *ptr1;
    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);
    result = (*ptr1) (a, b, c);

    printf("%p %p %p", a, b, c);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM