Program 1: Structure to store and display student details
#include <stdio.h>
struct Student {
char name[50];
int age;
float marks;
};
int main() {
struct Student s;
printf("Enter name: ");
scanf("%s", [Link]);
printf("Enter age: ");
scanf("%d", &[Link]);
printf("Enter marks: ");
scanf("%f", &[Link]);
printf("\n--- Student Details ---\n");
printf("Name: %s\n", [Link]);
printf("Age: %d\n", [Link]);
printf("Marks: %.2f\n", [Link]);
return 0;
Program 2: Structure with array of structures (store 3 employees)
#include <stdio.h>
struct Employee {
int id;
char name[50];
float salary;
};
int main() {
struct Employee e[3];
int i;
for(i = 0; i < 3; i++) {
printf("\nEnter details for employee %d\n", i + 1);
printf("ID: ");
scanf("%d", &e[i].id);
printf("Name: ");
scanf("%s", e[i].name);
printf("Salary: ");
scanf("%f", &e[i].salary);
printf("\n--- Employee Details ---\n");
for(i = 0; i < 3; i++) {
printf("\nEmployee %d\n", i + 1);
printf("ID: %d\n", e[i].id);
printf("Name: %s\n", e[i].name);
printf("Salary: %.2f\n", e[i].salary);
return 0;
Program 3: Structure with function (display book details)
#include <stdio.h>
struct Book {
char title[50];
char author[50];
float price;
};
void display(struct Book b) {
printf("\n--- Book Details ---\n");
printf("Title : %s\n", [Link]);
printf("Author: %s\n", [Link]);
printf("Price : %.2f\n", [Link]);
int main() {
struct Book b;
printf("Enter title: ");
scanf("%s", [Link]);
printf("Enter author: ");
scanf("%s", [Link]);
printf("Enter price: ");
scanf("%f", &[Link]);
display(b);
return 0;
Program 4: Nested structure (address inside student)
#include <stdio.h>
struct Address {
char city[50];
int pincode;
};
struct Student {
char name[50];
int rollNo;
struct Address addr; // nested structure
};
int main() {
struct Student s;
printf("Enter name: ");
scanf("%s", [Link]);
printf("Enter roll number: ");
scanf("%d", &[Link]);
printf("Enter city: ");
scanf("%s", [Link]);
printf("Enter pincode: ");
scanf("%d", &[Link]);
printf("\n--- Student Details ---\n");
printf("Name : %s\n", [Link]);
printf("Roll No.: %d\n", [Link]);
printf("City : %s\n", [Link]);
printf("Pincode : %d\n", [Link]);
return 0;
#include <stdio.h>
void swap(int *a, int *b) {
int temp;
temp = *a;
*a = *b;
*b = temp;
int main() {
int x = 5, y = 10;
printf("Before swap: x = %d, y = %d\n", x, y);
swap(&x, &y);
printf("After swap : x = %d, y = %d\n", x, y);
return 0;
Homework Questions:
Find the largest and smallest number in an array using functions
Pointer to a structure:
struct Student {
int id;
};
struct Student s;
struct Student *p = &s;
[Link] = 1; // dot
p->id = 2; // arrow
(*p).id = 3; // equivalent to arrow
Used when the size is not fixed at compile time.
struct Student {
int id;
char *name; // pointer member
};
struct Student s;
[Link] = (char *)malloc(20 * sizeof(char));
Passing structures by pointer avoids copying large data.
struct Data {
int arr[1000];
};
void process(struct Data *d) { // pointer used
d->arr[0] = 10;
When number of records is not known.
struct Student *s;
int n = 5;
s = (struct Student *)malloc(n * sizeof(struct Student));
Returning pointer avoids copying entire structure.
struct Student* createStudent() {
struct Student *s = malloc(sizeof(struct Student));
return s;
Example 1:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Student {
int id;
char *name; // pointer inside structure
};
int main() {
struct Student s;
[Link] = 1;
[Link] = (char *)malloc(20 * sizeof(char));
strcpy([Link], "Ravi");
printf("ID: %d\n", [Link]);
printf("Name: %s\n", [Link]);
free([Link]);
return 0;
Example 2:
#include <stdio.h>
struct Data {
int arr[1000];
};
void modify(struct Data *d) {
d->arr[0] = 999;
int main() {
struct Data d;
[Link][0] = 10;
printf("Before: %d\n", [Link][0]);
modify(&d);
printf("After: %d\n", [Link][0]);
return 0;
Example 3:
#include <stdio.h>
#include <stdlib.h>
struct Student {
int id;
float marks;
};
int main() {
struct Student *s;
int n, i;
printf("Enter number of students: ");
scanf("%d", &n); // number not known at compile time
/* Dynamic allocation */
s = (struct Student *)malloc(n * sizeof(struct Student));
if (s == NULL) {
printf("Memory allocation failed\n");
return 1;
}
/* Input student details */
for (i = 0; i < n; i++) {
printf("\nStudent %d\n", i + 1);
printf("Enter ID: ");
scanf("%d", &s[i].id);
printf("Enter Marks: ");
scanf("%f", &s[i].marks);
/* Display student details */
printf("\nStudent Details:\n");
for (i = 0; i < n; i++) {
printf("ID: %d, Marks: %.2f\n", s[i].id, s[i].marks);
/* Free allocated memory */
free(s);
return 0;
Example 4:
#include <stdio.h>
struct Student {
int id;
char name[20];
};
void display(struct Student s[], int n) { // array → reference
s[0].id = 999; // affects original
for(int i = 0; i < n; i++) {
printf("ID: %d, Name: %s\n", s[i].id, s[i].name);
int main() {
struct Student s[2] = {
{101, "Anu"},
{102, "Bala"}
};
display(s, 2);
printf("\nAfter function call:\n");
printf("ID: %d, Name: %s\n", s[0].id, s[0].name);
return 0;
Example 5:
#include <stdio.h>
struct Student {
int id;
char name[20];
};
void display(struct Student *s, int n) { // pointer form
for(int i = 0; i < n; i++) {
printf("ID: %d, Name: %s\n", s[i].id, s[i].name);
}
}
int main() {
struct Student s[2] = {
{101, "Anu"},
{102, "Bala"}
};
display(s, 2);
return 0;
How Effect on
Case
passed original
Single structure By value ❌ Not affected
Array of By
✔️Affected
structures reference
Pointer to By
✔️Affected
structure reference
Example 6:
#include <stdio.h>
void display(int *arr, int n) { // pointer parameter
for(int i = 0; i < n; i++) {
printf("%d ", arr[i]); // same as *(arr + i)
int main() {
int a[] = {10, 20, 30, 40};
display(a, 4); // array name passed
return 0;
Remember : arr[i] ≡ *(arr + i)
Example 7:
#include <stdio.h>
void modify(int *arr, int n) {
arr[0] = 999; // modifies original array
int main() {
int a[] = {10, 20, 30};
modify(a, 3);
printf("%d\n", a[0]);
return 0;
void display(int arr[], int n)
void display(int *arr, int n)
Example 8
#include <stdio.h>
void display(int (*arr)[3], int rows) {
for(int i = 0; i < rows; i++) {
for(int j = 0; j < 3; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
int main() {
int a[2][3] = {{1,2,3},{4,5,6}};
display(a, 2);
return 0;