C Programming Questions and Answers – Pointer to Structures – 1

«
»

This set of C Multiple Choice Questions & Answers (MCQs) focuses on “Pointer to Structures – 1”.

Pre-requisite for this C MCQ set: Advanced C Programming Video Tutorial.

advertisement

1. What will be the output of the following C code?

  1.     #include <stdio.h>
  2.     struct p
  3.     {
  4.         int x;
  5.         char y;
  6.     };
  7.     int main()
  8.     {
  9.         struct p p1[] = {1, 92, 3, 94, 5, 96};
  10.         struct p *ptr1 = p1;
  11.         int x = (sizeof(p1) / 3);
  12.         if (x == sizeof(int) + sizeof(char))
  13.             printf("%d\n", ptr1->x);
  14.         else
  15.             printf("falsen");
  16.     }

a) Compile time error
b) 1
c) Undefined behaviour
d) false
View Answer

Answer: d
Explanation: None.
advertisement
advertisement

2. What will be the output of the following C code?

advertisement
  1.     #include <stdio.h>
  2.     struct p
  3.     {
  4.         int x;
  5.         char y;
  6.     };
  7.     int main()
  8.     {
  9.         struct p p1[] = {1, 92, 3, 94, 5, 96};
  10.         struct p *ptr1 = p1;
  11.         int x = (sizeof(p1) / sizeof(ptr1));
  12.         if (x == 1)
  13.             printf("%d\n", ptr1->x);
  14.         else
  15.             printf("false\n");
  16.     }

a) Compile time error
b) 1
c) false
d) Undefined behaviour
View Answer

Answer: c
Explanation: None.
advertisement

3. What will be the output of the following C code?

  1.     #include <stdio.h>
  2.     struct p
  3.     {
  4.         int x;
  5.         char y;
  6.     };
  7.     typedef struct p* q*;
  8.     int main()
  9.     {
  10.         struct p p1[] = {1, 92, 3, 94, 5, 96};
  11.         q ptr1 = p1;
  12.         printf("%d\n", ptr1->x);
  13.     }

a) Compile time error
b) 1
c) Undefined behaviour
d) Segmentation fault
View Answer

Answer: a
Explanation: None.
advertisement

4. What will be the output of the following C code?

  1.     #include <stdio.h>
  2.     struct p
  3.     {
  4.         int x;
  5.         char y;
  6.     };
  7.     void foo(struct p* );
  8.     int main()
  9.     {
  10.         typedef struct p* q;
  11.         struct p p1[] = {1, 92, 3, 94, 5, 96};
  12.         foo(p1);
  13.     }
  14.     void foo(struct p* p1)
  15.     {
  16.         q ptr1 = p1;
  17.         printf("%d\n", ptr1->x);
  18.     }

a) Compile time error
b) 1
c) Segmentation fault
d) Undefined behaviour
View Answer

Answer: a
Explanation: None.

5. Which of the following is an incorrect syntax for pointer to structure?

(Assuming struct temp{int b;}*my_struct;)

a) *my_struct.b = 10;
b) (*my_struct).b = 10;
c) my_struct->b = 10;
d) Both *my_struct.b = 10; and (*my_struct).b = 10;
View Answer

Answer: a
Explanation: None.

6. Which of the following is an incorrect syntax to pass by reference a member of a structure in a function?

(Assume: struct temp{int a;}s;)

a) func(&s.a);
b) func(&(s).a);
c) func(&(s.a));
d) none of the mentioned
View Answer

Answer: d
Explanation: None.

7. Which of the following structure declaration doesn’t require pass-by-reference?
a)

   struct{int a;}s;
   main(){}

b)

   struct temp{int a;};
   main(){
       struct temp s;
   }

c)

   struct temp{int a;};
   main(){}
   struct temp s;

d) none of the mentioned
View Answer

Answer: d
Explanation: None.

8. Which option is not possible for the following function call?

  1. func(&s.a); //where s is a variable of type struct and a is the member of the struct.

a) Compiler can access entire structure from the function
b) Individual member’s address can be displayed in structure
c) Individual member can be passed by reference in a function
d) None of the mentioned
View Answer

Answer: a
Explanation: None.

9. What will be the output of the following C code?

  1.     #include <stdio.h>
  2.     struct temp
  3.     {
  4.         int a;
  5.     } s;
  6.     void change(struct temp);
  7.     main()
  8.     {
  9.         s.a = 10;
  10.         change(s);
  11.         printf("%d\n", s.a);
  12.     }
  13.     void change(struct temp s)
  14.     {
  15.         s.a = 1;
  16.     }

a) Output will be 1
b) Output will be 10
c) Output varies with machine
d) Compile time error
View Answer

Answer: b
Explanation: None.

10. What will be the output of the following C code?

  1.  #include <stdio.h>
  2.     struct p
  3.      {
  4.         int x;
  5.         int y;
  6.     };
  7.     int main()
  8.     {
  9.         struct p p1[] = {1, 92, 3, 94, 5, 96};
  10.         struct p *ptr1 = p1;
  11.         int x = (sizeof(p1) / 5);
  12.         if (x == 3)
  13.             printf("%d %d\n", ptr1->x, (ptr1 + x - 1)->x);
  14.         else
  15.             printf("false\n");
  16.     }

a) Compile time error
b) 1 5
c) Undefined behaviour
d) false
View Answer

Answer: d
Explanation: None.

Sanfoundry Global Education & Learning Series – C Programming Language.

Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

advertisement
advertisement

Leave a Comment

Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter

Check his free tutorial videos on Advanced C Programming. Join his Sanfoundry-Classes telegram channel for all videos and queries on Programming, Data-Structures, Kernel, Personality development and Mentoring.