Lesson 22 of 23

Structures (struct)

11 min 35 XP

A struct groups fields of different types into a new type, like a student's name, roll number and marks. typedef gives it a short name, and -> accesses a field through a pointer.

Example: edit me and press Run

Output Loading C (first time takes a few seconds)
Press Run to see the result here.

struct vs union: in a union all fields share the same memory. Expect this question in C interviews.

Challenge

+35 XP

Read n, then n students' names and marks (int). Store them in an array of structs and print the topper and the class average. Input: 3 Arun 78 Bala 95 Charu 88 Output: Topper: Bala 95 Average: 87.00

Output Loading C (first time takes a few seconds)
Press Run to see the result here.