Structures
- What will be the output of the following C code?
#include <stdio.h>
struct Employee
{
char *Name;
};
struct Employee fun(void)
{
struct Employee emp1;
emp1.Name = "Prayag";
return emp1;
}
void main()
{
struct Employee emp2 = fun();
emp1.Name = "Ajit";
printf("%s", emp2.Name);
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: C
Compilation Error
main.c: In function ‘main’:
main.c:15:9: error: ‘emp1’ undeclared (first use in this function); did you mean ‘emp2’?
emp1.Name = "Ajit";
^~~~
emp2
main.c:15:9: note: each undeclared identifier is reported only once for each function it appears in
- Which of the following is not possible under any scenario?
-
View Hint View Answer Discuss in Forum
NA
Correct Option: E
n1 = n2; or (*n1).number = 10; and n1 = &n2; are not possible under any scenario.
- Which of the following return-type cannot be used for a function in C?
-
View Hint View Answer Discuss in Forum
NA
Correct Option: E
struct, void and char * return-type cannot be used for a function in C.
- What will be the output of the following C code?
#include <stdio.h>
struct test
{
int num;
} t;
void func(struct test t)
{
t.num = 125;
printf("%d ", t.num);
}
main()
{
func(t);
printf("%d ", t.num);
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: B
125 0
- What will be the output of the following C code?
#include <stdio.h>
struct furniture
{
int a;
int b;
};
void fun(struct furniture*);
int main()
{
struct furniture frn1 = {12, 22};
fun(&frn1);
}
void fun(struct point *ptr)
{
printf("%d\n", *ptr.a++);
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: A
Compilation Error
main.c:13:21: warning: ‘struct point’ declared inside parameter list will not be visible outside of this definition or declaration
void fun(struct point *ptr)
^~~~~
main.c:13:10: error: conflicting types for ‘fun’
void fun(struct point *ptr)
^~~
main.c:7:10: note: previous declaration of ‘fun’ was here
void fun(struct furniture*);
^~~
main.c: In function ‘fun’:
main.c:15:28: error: ‘ptr’ is a pointer; did you mean to use ‘->’?
printf("%d\n", *ptr.a++);
^