Typedef
- 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;
}
-
View Hint View Answer Discuss in Forum
NA
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]
- What will be the output of the following C code?
#include <stdio.h>
typedef struct MM
{
int n1, n2;
}mm;
int main()
{
struct MM m = {22, 44};
mm k = m;
printf("%d\n", k.n1);
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: C
22
- What will be the output of the following C code?
#include <stdio.h>
typedef struct NN
{
int n, m;
}s = {11, 22};
int main()
{
NN nn = s;
printf("%d\n", nn.n);
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: E
Compilation Error
main.c:5:5: error: typedef ‘s’ is initialized (use __typeof__ instead)
}s = {11, 22};
^
main.c: In function ‘main’:
main.c:8:9: error: unknown type name ‘NN’; use ‘struct’ keyword to refer to the type
NN nn = s;
^~
struct
main.c:8:17: error: expected expression before ‘s’
NN nn = s;
^
main.c:9:26: error: request for member ‘n’ in something not a structure or union
printf("%d\n", nn.n);
- What will be the output of the following C code?
#include <stdio.h>
typedef struct B
{
int n1, n2;
};
int main()
{
B k = {10, 20};
printf("%d\n", k.n1);
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: C
Compilation Error
main.c:5:5: warning: useless storage class specifier in empty declaration
};
^
main.c: In function ‘main’:
main.c:8:9: error: unknown type name ‘B’; use ‘struct’ keyword to refer to the type
B k = {10, 20};
^
struct
main.c:8:20: warning: excess elements in scalar initializer
B k = {10, 20};
^~
main.c:8:20: note: (near initialization for ‘k’)
main.c:9:25: error: request for member ‘n1’ in something not a structure or union
printf("%d\n", k.n1);
- What will be the output of the following C code?
#include <stdio.h>
typedef struct Employee
{
char *ch;
}Employ;
void main()
{
struct Employee emp;
emp.ch = "Welcome";
printf("%s", emp.ch);
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: A
Welcome