函数指针作为C中的参数

时间:2022-06-29 21:44:37

Hi I've been looking over * and I'm really struggling with function pointers as parameters.

嗨,我一直在查看*,我真的很难将函数指针作为参数。

I have the structure:

我有结构:

 struct Node {
     struct Node *next;
     short len;
     char data[6];
 };

and the functions:

和功能:

 void selectionsort(int (*compareData)(struct Node *, struct Node *), void (*swapData)(struct Node *, struct Node *), int n);

 compare(struct Node *a, struct Node *b);
 swap(struct Node *a, struct Node *b);

selection sort serves only to call compare and swap:

选择排序只用于调用比较和交换:

 void selectionsort(int compare(struct Node *a, struct Node *b), void swap(struct Node *a, struct Node *b), int n){
     int i;
     for (i = 0; i < n; i++){
         compare(a, b);
         swap(a, b);
     }
 }

(the contents above may be incorrect, I have yet to get to fooling around with the actual selectionsort function really).

(上面的内容可能不正确,我还没有真正去实际的选择函数)。

The problem arises when I call selectionsort in main. I was under the impression this would work:

当我在main中调用selectionsort时会出现问题。我认为这会起作用:

 int main(int argc, char **argv){
     int n;
     struct Node *list = NULL;
     for (n = 1; n < argc; n++)
         list = prepend(list, argv[n]); //separate function not given here.
     int (*compareData)(struct Node *, struct Node *) = compare; //not sure I needed to redeclare this
     void (*swapData)(struct Node *, struct Node *) = swap;
     selectionsort(compareData(list, list->next), swapData(list, list->next), argc);

     //other stuff
     return 0;
 }

Note: the function prepend contains the malloc for the struct, so that's been dealt with.

注意:函数prepend包含struct的malloc,因此已经处理过了。

The problem I'm having is no matter how I play around with my function declarations etc, I always get the following error:

我遇到的问题是无论我如何玩我的函数声明等,我总是得到以下错误:

warning: passing argument 1 of 'selectionsort' makes pointer from integer without a cast [enabled by default]
note: expected 'int (*)(struct Node *, struct Node *)' but argument is of type 'int'.

Any help explaining why I'm getting this error message and how to fix it would be immensely appreciated.

任何帮助解释为什么我收到此错误消息以及如何解决它将非常感激。

I understand that the function is expecting a function pointer but I assumed my code above would allow for compareto be called. Any input would be greatly appreciated, also this is for an assignment (so please help me avoid cheating) and the parameters int(*compareData)(struct Node *, struct Node *) void (*swapData)(struct Node *, struct Node *) were given.

我理解该函数期待一个函数指针但我假设我的代码上面允许调用compare。任何输入都会非常感激,也是为了一个赋值(所以请帮我避免作弊)和参数int(* compareData)(struct Node *,struct Node *)void(* swapData)(struct Node *,struct Node) *) 被给予。

2 个解决方案

#1


2  

In your call to selectionsort you are actually calling those function pointers, causing you to pass the result of those calls to selectionsort. They are normal variables, and should be passed to selectionsort like any other variable argument.

在您对selectionsort的调用中,您实际上正在调用这些函数指针,导致您将这些调用的结果传递给selectionsort。它们是正常变量,应该像任何其他变量参数一样传递给selectionsort。

However, you don't actually need the variables, you can just pass the functions directly:

但是,您实际上并不需要变量,您可以直接传递函数:

selectionsort(&compare, &swap, argc);

Note that the address-of operator is not strictly needed, but I prefer to use them to explicitly tell the reader that we are passing pointers to those functions.

请注意,不严格需要address-of运算符,但我更喜欢使用它们来明确地告诉读者我们正在传递指向这些函数的指针。

#2


3  

In

selectionsort(compareData(list, list->next), swapData(list, list->next), argc);

you are passing the result of calling the function compareData. This is wrong.

您正在传递调用函数compareData的结果。这是错的。

The same holds for swapData.

对于swapData也是如此。

Just pass the function itself (resp. a pointer to it):

只需传递函数本身(相应的指向它的指针):

selectionsort(compareData, swapData, argc);

#1


2  

In your call to selectionsort you are actually calling those function pointers, causing you to pass the result of those calls to selectionsort. They are normal variables, and should be passed to selectionsort like any other variable argument.

在您对selectionsort的调用中,您实际上正在调用这些函数指针,导致您将这些调用的结果传递给selectionsort。它们是正常变量,应该像任何其他变量参数一样传递给selectionsort。

However, you don't actually need the variables, you can just pass the functions directly:

但是,您实际上并不需要变量,您可以直接传递函数:

selectionsort(&compare, &swap, argc);

Note that the address-of operator is not strictly needed, but I prefer to use them to explicitly tell the reader that we are passing pointers to those functions.

请注意,不严格需要address-of运算符,但我更喜欢使用它们来明确地告诉读者我们正在传递指向这些函数的指针。

#2


3  

In

selectionsort(compareData(list, list->next), swapData(list, list->next), argc);

you are passing the result of calling the function compareData. This is wrong.

您正在传递调用函数compareData的结果。这是错的。

The same holds for swapData.

对于swapData也是如此。

Just pass the function itself (resp. a pointer to it):

只需传递函数本身(相应的指向它的指针):

selectionsort(compareData, swapData, argc);