I have been asked to make a code that will rearrange 3 entered integers into ascending/descending order using pointers.
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.
I keep getting an error on the line specified in the readInts() function and am not sure how to fix it.
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). 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.
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).
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):
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);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.