0

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;
}
Chuan
  • 429
  • 5
  • 16
  • 1
    @Neo C++ has both. You can still use `printf` in C++. – melpomene Oct 30 '17 at 12:23
  • 1
    @Neo C++ is not compatible with C, but the code posted here is valid C++. – melpomene Oct 30 '17 at 12:24
  • 1
    It's **not** compatible. This code happens to work *as C++* as well, as far as I can see. Still this question doesn't seem to relate to C++ at all, so tag removed. –  Oct 30 '17 at 12:25
  • "*Then (*function) is the same as function.*" you should explain what you mean here. Also, `qsort(A, 5, sizeof(*A), *cmp);` should be a compile error, you can't dereference a function (`cmp`). –  Oct 30 '17 at 12:26
  • @FelixPalmen Of course you can dereference a function. It's just like you can dereference an array. – melpomene Oct 30 '17 at 12:27
  • @melpomene you're right on that one (well, not exactly like arrays, but nevermind). Still, don't spam unrelated tags. –  Oct 30 '17 at 12:31
  • 1
    @FelixPalmen Don't remove relevant tags. This is a valid C++ question. – melpomene Oct 30 '17 at 12:31
  • 1
    @Chuanyuan should clarify if the question refers to C or C++. It could be either one. – blue Oct 30 '17 at 12:32
  • 1
    Your premise is faulty, if `int (*function) (int arg)`, then `&function` is not the same as `function`. – eerorika Oct 30 '17 at 12:33
  • 1
    @melpomene it's not, community concensus is that a question needs to be about interfacing C and C++ or a direct comparison to qualify for **both** tags. This code is C style and doesn't mention anything about C++, so it's a C question, EOD. –  Oct 30 '17 at 12:34
  • @FelixPalmen Should I edit the tags from linked OP? It has a `std::cout` call and has tags as C++ and C. – Amadeus Oct 30 '17 at 12:35
  • @Amadeus no, we shouldn't touch ages-old questions IMHO. Nowadays you would ask for clarification first (if any doubts arise like clearly mixing C and C++ concepts) to avoid this kind of tag-spam. –  Oct 30 '17 at 12:36
  • @FelixPalmen Well, clearly the community is wrong on this one. – melpomene Oct 30 '17 at 12:36
  • 1
    @melpomene that's your opinion, a majority is in favor of avoiding unnecessary tags. There are corner cases, but with code that doesn't show a trace of typical C++, the tag is wrong and probably the result of too many people not being aware there's a difference. –  Oct 30 '17 at 12:38

0 Answers0