1

I write three different type define but they executes same result. so why is this happening. I thought the result might be different because I used the same way to execute the function.


#include <stdio.h>

int* digit(int* number){
    return number;
}

typedef int*function0(int*);
typedef int*(function1(int*));
typedef int*(*function_pointer)(int*);

void printDigit0(function0 p, int* digit){
    printf("%d", *p(digit));
}

void printDigit1(function1 p, int* digit){
    printf("%d", *p(digit));
}

void printDigit2(function_pointer p, int* digit){
    printf("%d", *p(digit));
}

int main() {

    int a = 10;
    printDigit0(digit, &a);
    printDigit1(digit, &a);
    printDigit2(digit, &a);
    return 0;
}


they all printed 10

Dilnar
  • 59
  • 2
  • All function pointer prototype are same i.e their input argument type & return type are exactly same. – Achal Aug 14 '19 at 03:28
  • See [Why do function pointer definitions work with any number of ampersands '&' or asterisks '*'?](https://stackoverflow.com/questions/6893285/why-do-function-pointer-definitions-work-with-any-number-of-ampersands-or-as) – Lundin Aug 14 '19 at 08:49

1 Answers1

0

function0 and function1 are the same, you just have redundant parentheses. function_pointer is a pointer to a function0.

In a function parameter if you specify a function type it's adjusted to be a function pointer type (i.e. the function behaves as if you had specified a function pointer type). So printDigit0 and printDigit1 behave as if they had the same declaration as printDigit2.

M.M
  • 138,810
  • 21
  • 208
  • 365