Home » C Programming » Typedef » Question
  1. What will be the output of the following C code?
    #include <stdio.h>
    int (*(a()))[2];
    typedef int (*(*p)())[2] ptrfun;
    int main()
    {
    ptrfun p1;
    p1 = a;
    p1();
    return 0;
    }
    int (*(a()))[2]
    {
    int (*array)[2] = malloc(sizeof*array);
    return &array;
    }
    1. Nothing
    2. Undefined behaviour
    3. Compilation Error
    4. Garbage value
    5. None of these
Correct Option: C

Compilation Error

main.c:3:30: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘ptrfun’
typedef int (*(*p)())[2] ptrfun;
^~~~~~
main.c: In function ‘main’:
main.c:6:9: error: unknown type name ‘ptrfun’
ptrfun p1;
^~~~~~
main.c:7:12: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
p1 = a;
^
main.c:8:9: error: called object ‘p1’ is not a function or function pointer
p1();
^~
main.c:6:16: note: declared here
ptrfun p1;
^~
main.c: In function ‘a’:
main.c:13:27: warning: implicit declaration of function ‘malloc’ [-Wimplicit-function-declaration]
int (*array)[2] = malloc(sizeof*array);
^~~~~~
main.c:13:27: warning: incompatible implicit declaration of built-in function ‘malloc’
main.c:13:27: note: include ‘’ or provide a declaration of ‘malloc’
main.c:14:16: warning: return from incompatible pointer type [-Wincompatible-pointer-types]
return &array;
^~~~~~
main.c:14:16: warning: function returns address of local variable [-Wreturn-local-addr]



Your comments will be displayed only after manual approval.