I know that the compiler automatically dereferences functions pointers.
Assume int (*function) (int arg);
Then (*function)
is the same as function. But why is &function
also the same as function? I don't understand why the following code works.
What is the correct way of passing function pointers?
When we declare a function, for example, int f(int a)
, does it mean f
is automatically the address in which the function is stored?
Thanks in advance.
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
int cmp (const void *a, const void *b) {
return *(int*)a-*(int*)b;
}
int main (int argc, char *argv[]) {
int i;
int A[5] = {0,2,3,1,4};
qsort(A, 5, sizeof(*A), cmp);
for (i = 0; i < 5; i++){
printf("%d,",A[i]);
}
printf("\n");
qsort(A, 5, sizeof(*A), &cmp);
for (i = 0; i < 5; i++){
printf("%d,",A[i]);
}
printf("\n");
qsort(A, 5, sizeof(*A), *cmp);
for (i = 0; i < 5; i++){
printf("%d,",A[i]);
}
printf("\n");
return 0;
}