Chapter 3: Structure & Union
1 Mark Questions
1) Define Structure.
→ Structure is a user-defined data type that allows to store variables of different
data types under a single name.
2) Write syntax to declare structure.
struct struct_name
{
data_type member1;
data_type member2;
...
};
3) What is union?
→ Union is a user-defined data type in which all members share the same memory
location.
4) What is nested structure?
→ A structure within another structure is called a nested structure.
5) Use of typedef keyword?
→ typedef is used to create a new name (alias) for existing data types.
2 Marks Questions
1) Difference between structure and union.
Structure Union
Separate memory for each member Shared memory for all members
All members can be used at a time Only one member can be used at a time
Size = Sum of all members Size = Size of largest member
2) What is array of structure?
→ Array of structure is a collection of structure variables of the same type.
3) Program to define structure of student.
struct student {
int roll;
char name[20];
float marks;
};
4) Explain structure with pointer.
→ Pointer can store the address of a structure variable and access members using ->
operator.
Example:
struct student s1 = {1, "John", 80};
struct student *ptr = &s1;
printf("%d", ptr->roll);
5) Explain structure with function.
→ Structure can be passed to a function either by value or by reference.
Example:
void display(struct student *s);
3 Marks Questions
1) Program to store and display student details using structure.
#include <stdio.h>
struct student {
int roll;
char name[20];
};
int main() {
struct student s;
printf("Enter roll and name: ");
scanf("%d %s", &[Link], [Link]);
printf("Roll: %d\nName: %s", [Link], [Link]);
return 0;
}
2) Program to use nested structure.
#include <stdio.h>
struct date {
int day, month, year;
};
struct student {
int roll;
char name[20];
struct date dob;
};
int main() {
struct student s = {1, "John", {10, 5, 2000}};
printf("Roll: %d\nName: %s\nDOB: %d-%d-%d", [Link], [Link], [Link],
[Link], [Link]);
return 0;
}
3) Program using array of structure.
#include <stdio.h>
struct student {
int roll;
char name[20];
};
int main() {
struct student s[3];
int i;
for(i = 0; i < 3; i++) {
printf("Enter roll and name: ");
scanf("%d %s", &s[i].roll, s[i].name);
}
for(i = 0; i < 3; i++) {
printf("Roll: %d Name: %s\n", s[i].roll, s[i].name);
}
return 0;
}
4) Explain advantages of structure.
→ Advantages:
Group related data together.
Efficient data handling.
Easy to pass complex data to functions.
Useful in real-life applications like student records, employee records.
5) Program to pass structure to function.
#include <stdio.h>
struct student {
int roll;
char name[20];
};
void display(struct student s) {
printf("Roll: %d Name: %s\n", [Link], [Link]);
}
int main() {
struct student s1 = {1, "John"};
display(s1);
return 0;
}
4 Marks Questions
1) Explain difference between structure and union with example.
Structure Union
Allocates separate memory to each member Allocates shared memory to all members
All members can store data simultaneously Only one member can store data at a time
Example:
struct student {
int roll;
float marks;
};
union student {
int roll;
float marks;
};
2) Program to store employee details using structure and pointer.
#include <stdio.h>
struct employee {
int id;
char name[20];
};
int main() {
struct employee e = {101, "Alice"};
struct employee *ptr = &e;
printf("ID: %d\nName: %s", ptr->id, ptr->name);
return 0;
}
3) Explain structure with function (pass by value and address).
void display(struct student s) { ... }
4) Explain typedef with example.
Explanation:
typedef creates alias name for existing type.
Example:
typedef struct student {
int roll;
char name[20];
} STUD;
STUD s1 = {1, "John"};
5) Program to store student marks and calculate average using structure.
#include <stdio.h>
struct student {
int roll;
char name[20];
float marks1, marks2, marks3;
};
int main() {
struct student s;
float avg;
printf("Enter roll, name, marks1, marks2, marks3: ");
scanf("%d %s %f %f %f", &[Link], [Link], &s.marks1, &s.marks2, &s.marks3);
avg = (s.marks1 + s.marks2 + s.marks3) / 3;
printf("Average Marks: %.2f", avg);
return 0;
}