IV :Array and Structure
Arrays in C
● An array is a collection of elements of the same data type
stored in contiguous memory locations. Each element can be
accessed using its index number.
Types of Arrays
1. One-Dimensional Array — stores data in a single row (linear
form).
2. Two-Dimensional Array — stores data in rows and columns
(matrix form).
1. One-Dimensional Array
Declaration:
data_type array_name[size];
Example:
int marks[5];
Initialization:
Method 1 – Full Initialization:
int marks[5] = {90, 85, 75, 80, 95};
Method 2 – Partial Initialization:
int marks[5] = {90, 85}; // remaining elements become 0
Method 3 – Implicit Size:
int marks[] = {90, 85, 75, 80, 95};
Example:
#include <stdio.h>
int main() {
int marks[5] = {90, 85, 75, 80, 95};
int i;
printf("Student Marks:\n");
for(i = 0; i < 5; i++) {
printf("%d\n", marks[i]);
}
return 0;
}
Output:
90
85
75
80
95
===============================================
2. Two-Dimensional Array
Declaration:
data_type array_name[rows][columns];
Example:
int matrix[3][3];
Initialization:
Method 1 – Full Initialization:
int matrix[2][3] = { {1, 2, 3},{4, 5, 6}};
Method 2 – Single Line:
int matrix[2][3] = {1, 2, 3, 4, 5, 6};
Example:
#include <stdio.h>
int main() {
int matrix[2][3] = { {1, 2, 3}, {4, 5, 6} };
printf("Matrix Elements:\n");
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 3; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
return 0;
}
Output:
123
456
One-Dimensional Array Examples in C
Example 1: Print all elements of an array
#include <stdio.h>int main() { int numbers[5] = {10, 20, 30, 40, 50}; int i;
printf("Array Elements:\n"); for(i = 0; i < 5; i++) { printf("%d\n", numbers[i]); }
return 0;}
Output:
10
20
30
40
50
Example 2: Find Sum of Array Elements
#include <stdio.h>int main() { int arr[5] = {10, 20, 30, 40, 50}; int sum = 0; for(int i =
0; i < 5; i++) { sum += arr[i]; } printf("Sum of array elements = %d\n", sum);
return 0;}
Output:
Sum of array elements = 150
Example 3: Find Maximum Element
#include <stdio.h>int main() { int arr[5] = {12, 45, 23, 67, 34}; int max = arr[0];
for(int i = 1; i < 5; i++) { if(arr[i] > max) max = arr[i]; } printf("Maximum
element = %d\n", max); return 0;}
Output:
Maximum element = 67
Example 4: Reverse Array Elements
#include <stdio.h>int main() { int arr[5] = {1, 2, 3, 4, 5}; printf("Array in reverse
order:\n"); for(int i = 4; i >= 0; i--) { printf("%d ", arr[i]); } return 0;}
Output:
Array in reverse order:
54321
Example 5: Input Elements and Display
#include <stdio.h>int main() { int arr[5]; int i; printf("Enter 5 numbers:\n"); for(i =
0; i < 5; i++) { scanf("%d", &arr[i]); } printf("You entered:\n"); for(i = 0; i < 5; i++)
{ printf("%d ", arr[i]); } return 0;}
Sample Output:
Enter 5 numbers:
12345
You entered:
12345
Two-Dimensional Array Examples in C
Example 1: Display a 2D Array (Matrix)
#include <stdio.h>int main() { int matrix[2][3] = { {1, 2, 3},{4, 5, 6} }; printf("Matrix
Elements:\n"); for(int i = 0; i < 2; i++) { for(int j = 0; j < 3; j++) { printf("%d ",
matrix[i][j]); } printf("\n"); } return 0;}
Output:
123
456
Example 2: Sum of All Elements in a 2D Array
#include <stdio.h>int main() { int a[2][2] = {{1, 2}, {3, 4}}; int sum = 0; for(int i = 0; i
< 2; i++) { for(int j = 0; j < 2; j++) { sum += a[i][j]; } } printf("Sum of all
elements = %d\n", sum); return 0;}
Output:
Sum of all elements = 10
Example 3: Matrix Addition
#include <stdio.h>int main() { int a[2][2] = {{1, 2}, {3, 4}}; int b[2][2] = {{5, 6}, {7, 8}};
int sum[2][2]; printf("Matrix Addition:\n"); for(int i = 0; i < 2; i++) { for(int j = 0; j <
2; j++) { sum[i][j] = a[i][j] + b[i][j]; printf("%d ", sum[i][j]); } printf("\
n"); } return 0;}
Output:
68
10 12
Example 4: Matrix Transpose
#include <stdio.h>
int main() {
int a[2][3] = {{1, 2, 3}, {4, 5, 6}};
int transpose[3][2];
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 3; j++) {
transpose[j][i] = a[i][j];
}
}
printf("Transpose of the matrix:\n");
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 2; j++) {
printf("%d ", transpose[i][j]);
}
printf("\n");
}
return 0;
}
Output:
14
25
36
Example 5: Matrix Multiplication
#include <stdio.h>int main() { int a[2][2] = {{1, 2}, {3, 4}}; int b[2][2] = {{2, 0}, {1, 2}};
int result[2][2] = {0}; for(int i = 0; i < 2; i++) { for(int j = 0; j < 2; j++) { for(int k
= 0; k < 2; k++) { result[i][j] += a[i][k] * b[k][j]; } } } printf("Matrix
Multiplication Result:\n"); for(int i = 0; i < 2; i++) { for(int j = 0; j < 2; j++)
{ printf("%d ", result[i][j]); } printf("\n"); } return 0;}
Output:
44
10 8
==============================================
Structure in C
Definition:
A structure in C is a user-defined data type that allows you to
combine data items of different data types under one name.
It is used to represent a record — for example, a student record that
contains a name, roll number, and marks.
🔹 Defining and Using a Structure
Syntax:
struct structure_name {
data_type member1;
data_type member2;
...
};
Example:
struct Student {
int roll_no;
char name[50];
float marks;
};
Here,
● struct Student is the structure name.
● It contains three members: roll_no, name, and marks.
🔹 Declaring Structure Variables
You can declare structure variables in two ways:
Method 1 – After structure definition:
struct Student s1, s2;
Method 2 – With structure definition:
struct Student {
int roll_no;
char name[50];
float marks;
} s1, s2;
🔹 Accessing Structure Members
Use the dot (.) operator to access structure members.
Example:
s1.roll_no = 1;
strcpy([Link], "Ravi");
[Link] = 85.5;
To print:
printf("Roll No: %d\n", s1.roll_no);
printf("Name: %s\n", [Link]);
printf("Marks: %.2f\n", [Link]);
Example 1: Simple Structure Program
#include <stdio.h>
#include <string.h>
struct Student {
int roll_no;
char name[50];
float marks;
};
int main() {
struct Student s1;
s1.roll_no = 101;
strcpy([Link], "Amit");
[Link] = 88.5;
printf("Student Details:\n");
printf("Roll No: %d\n", s1.roll_no);
printf("Name: %s\n", [Link]);
printf("Marks: %.2f\n", [Link]);return 0;}
Output:
Student Details:
Roll No: 101
Name: Amit
Marks: 88.50
🔹 Array of Structures
We can create an array of structures to store multiple records.
Syntax:
struct structure_name variable_name[size];
Example:
struct Student students[3];
Example 2: Array of Structures
#include <stdio.h>
#include <string.h>
struct Student {
int roll_no;
char name[50];
float marks;
};
int main() {
struct Student s[3];
// Input data
for(int i = 0; i < 3; i++) {
printf("Enter Roll No, Name, and Marks for student %d:\n",
i+1);
scanf("%d", &s[i].roll_no);
scanf("%s", s[i].name);
scanf("%f", &s[i].marks);
} // Display data
printf("\nStudent Details:\n");
for(int i = 0; i < 3; i++) {
printf("\nRoll No: %d\n", s[i].roll_no);
printf("Name: %s\n", s[i].name);
printf("Marks: %.2f\n", s[i].marks);
}
return 0;
}
Sample Output:
Enter Roll No, Name, and Marks for student 1:
101 Amit 88.5
Enter Roll No, Name, and Marks for student 2:
102 Raj 92.0
Enter Roll No, Name, and Marks for student 3:
103 Neha 85.0
Student Details:
Roll No: 101
Name: Amit
Marks: 88.50
Roll No: 102
Name: Raj
Marks: 92.00
Roll No: 103
Name: Neha
Marks: 85.00
🔹 Example 3: Structure with Function
#include <stdio.h>
#include <string.h>
struct Student {
int roll_no;
char name[50];
float marks;
};
void display(struct Student s) {
printf("Roll No: %d\n", s.roll_no);
printf("Name: %s\n", [Link]);
printf("Marks: %.2f\n", [Link]);
}
int main() {
struct Student s1 = {101, "Amit", 88.5};
printf("Student Information:\n");
display(s1);
return 0;
}
Output:
Student Information:
Roll No: 101
Name: Amit
Marks: 88.50