English 中文(简体)
作为C[重复]论点通过职能
原标题:passing functions as arguments in C [duplicate]
  • 时间:2012-05-07 22:25:27
  •  标签:
  • c

我看一看我的教科书之一,我看到C功能的以下编码线:

node_t* func(node_t* n, int f(node_t *))

What exactly does this mean? There is a function as an argument in there. What is the point of that? Shouldn t you just be able to call any function in the file as long as it s been declared? Also, in terms of assembly, is it the memory location of int f() that is being referenced?

谢谢。

最佳回答
node_t* func(node_t* n, int f(node_t *))

这是一项功能,称为“func”,具有两个参数:N, no子, 和 f, 功能的点子, 即 no子和 in子。 该功能使一名点人返回到 no。

无论如何,最常用的是通用算法。

“只要被宣布,你就能够把档案中的任何功能称为吗?”

只要在汇编股宣布这一职能,而且联系人可以发现这一职能是正确的。 然而,当你想要在运行时间决定什么功能使用时,就使用职能点(或如果你只想履行一般职能,则汇编时间)。

http://www.cplus.com/vis/clibrary/cstdlib/qsort/“rel=“nofollow”>qsort :

void qsort ( void * base, size_t num, size_t size, int ( * comparator ) ( const void *, const void * ) );

Now consider this:

typedef struct Student { int id; char* name; }

Student students[10];

//populate students

int sortByName(void* a, void* b)
{
    Student* sa = a;
    Student* sb = b;
    return strcmp(a->name, b->name);
}

int sortById(void* a, void* b)
{
    Student* sa = a;
    Student* sb = b;
    return a->id - b->id;
}

//sort by name:
qsort(students, 10, sizeof(Student), sortByName);

//sort by id:
qsort(students, 10, sizeof(Student), sortById);

重要的是,分类法必须改变。 分类的实施实际上是通用的。 它是按不同数据类型操作的一种算法,在这种情况下,这种通用性由一个职能点加以促进。

职能点的其他用途(占少数),如背书,或根据地图划分功能。

问题回答

The second argument is a function pointer to a function with signature int(node_t *). This way you can pass a call-back to func, like this:

int foo(node_t * p) { /* ... */ }

node_t n;
func(&n, foo);   //  note: the second argument is the function pointer

这里是一个非常顽固的例子,用来绕过一个虚构的阵容:

T * array_next(T * arr, int n) { return arr + n; }
T * array_prev(T * arr, int n) { return arr - n; }

T * move_in_array(T * arr, T * (*f)(T *, int))
{
    return f(arr, 1);
}

现在,你可以使用背书的方式,用时间确定的方式书写阵列上的代码:

T * p = move_in_array(some_array, flag ? array_next : array_prev);

这里的主要设计思想是,我们有一个通用的行动职能move_in_array,其具体实施作为职能联络点的一种理由。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签