Prepared By : Prof. Uday Shah (HOD-IT)
I.M.C.A Semester – 1 C Language Unit – 4 |
Unit -4: C Working
with Arrays
Contents
1. Introduction About an
Array
2. Declaration and
Initialization of an Array
5. Array with Functions
Using UDF
7. Declaration and
Initialization of Structure
Working with Arrays and Structures in C
1. Introduction About an Array
- An array is a collection of
elements of the same data type stored in contiguous memory locations.
- It allows storing multiple values
under a single variable name.
- Each element is accessed using an index,
starting from 0.
- Arrays simplify program design by
reducing the need for multiple variables.
- Example: Instead of using int a1,
a2, a3…, we use int arr[10];.
- Arrays can store integers, floats,
characters, or even structures.
- Arrays provide sequential
access making them efficient for iteration.
- They are static in size,
meaning once declared, the size cannot be changed.
- Useful in scenarios like storing
marks of students, salaries, etc.
- Example:
- int
marks[5] = {90, 85, 78, 88, 95};
- Here, marks[0] = 90 and marks[4] =
95.
- Declaration tells the compiler the
type and size of the array.
- Syntax: datatype arrayName[size];.
- Initialization assigns values at
the time of declaration.
- Example: int arr[5] =
{10,20,30,40,50};.
- If not fully initialized,
remaining elements are set to 0.
- Example: int arr[5] = {1,2}; //
rest = 0.
- Array elements can also be
initialized individually after declaration.
- Example:
- int
arr[3];
- arr[0]
= 10; arr[1] = 20; arr[2] = 30;
- Array size must be known at
compile time (unless using dynamic memory).
- Provides structured storage
for multiple values.
- Used in data management,
searching, sorting, etc.
- Array elements are accessed using index
numbers.
- Index starts from 0 (first
element).
- Example: arr[0] refers to the
first element, arr[n-1] refers to the last.
- Elements can be read/written
inside loops.
- Example:
- for(int
i=0; i<5; i++)
- printf("%d ", arr[i]);
- Elements can be updated: arr[2] =
50;.
- Accessing outside the range (out
of bounds) leads to undefined behavior.
- Array indexing provides random
access → direct access to any element.
- Useful in searching, sorting, and
mathematical operations.
- Accessing is O(1) operation
(constant time).
Arrays
are classified into different types:
(a)
One Dimensional Array
- A list of elements in a single
row/line.
- Example: int arr[5] =
{1,2,3,4,5};.
- Accessed using a single index.
- Useful for storing linear data
(marks, ages, etc.).
(b)
Two Dimensional Array
- Represents data in rows and
columns (matrix form).
- Declared as: int arr[3][3];.
- Example:
- int
matrix[2][2] = {{1,2}, {3,4}};
- Accessed using two indexes:
matrix[0][1].
(c)
Multi Dimensional Array
- Arrays with 3 or more
dimensions.
- Used in mathematical models, 3D
graphics, etc.
- Example: int arr[3][3][3];.
(d)
Character Array (String)
- A collection of characters stored
in sequence.
- Ends with \0 (null character).
- Example: char name[10] =
"Hello";.
- Strings are widely used in text
processing.
- Arrays can be passed to
functions for processing.
- Since arrays are passed by
reference, functions can modify original values.
- Example:
- void
display(int arr[], int n) {
- for(int i=0; i<n; i++) printf("%d
", arr[i]);
- }
- int
main() {
- int a[5] = {1,2,3,4,5};
- display(a,5);
- }
- Functions make array operations
modular and reusable.
- Common operations: sum, average,
search, sort.
- Passing arrays helps in handling large
data efficiently.
- Useful in modular programming with
clean code.
- Advantage: Reduces redundancy in
code.
- A structure is a
user-defined data type in C.
- It groups different types of
variables under a single name.
- Example: A student has id, name,
marks, which can be grouped in a structure.
- Declared using the struct keyword.
- Example:
- struct
Student {
- int id;
- char name[20];
- float marks;
- };
- Structures improve data
organization.
- Unlike arrays, they can store heterogeneous
data types.
- Widely used in real-world
applications like databases.
- They support nested structures,
arrays inside structures, etc.
- Declared using struct keyword
followed by members.
- Example:
- struct
Student {
- int id;
- char name[20];
- };
- Structure variables can be
declared in two ways:
- Along with structure definition.
- Separately in main().
- Initialization:
- struct
Student s1 = {101, "Raj"};
- Access members using the dot (.)
operator: s1.id, s1.name.
- Default values are not assigned;
must be initialized.
- Allows structured storage of
multiple attributes.
- Makes handling related data
easier.
- A structure inside another
structure is called nested structure.
- Helps in organizing hierarchical
data.
- Example:
- struct
Address {
- char city[20];
- int pincode;
- };
- struct
Student {
- int id;
- char name[20];
- struct Address addr;
- };
- Access: s1.addr.city.
- Useful in real applications like
storing employee info with addresses.
- Increases flexibility of
structures.
- Can combine multiple small
structures into a bigger one.
- Reduces complexity in
database-like programs.
- Used in complex data modeling.
- Structures can hold arrays as
members.
- Example:
- struct Student {
-
int id;
-
char name[20];
-
int marks[5]; // array inside structure
- };
- Useful for storing multiple values
related to one entity.
- Example: storing 5 subjects’ marks
of one student.
- Access: s1.marks[0].
- Provides structured storage of
grouped array data.
- Improves program organization.
- Useful in real-world tasks like storing transaction details.
- A collection of multiple structure
variables stored in an array.
- Example:
- struct Student {
-
int id;
-
char name[20];
- } s[3];
- Stores multiple students’ records
in a single array.
- Access: s[0].id, s[1].name.
- Very useful for handling large
datasets like employee or student records.
- Combines both arrays and
structures.
- Reduces memory waste by storing
multiple objects systematically.
- Helps in building databases
in C.
- Extensively used in practical
applications.
- Structures can be passed to
functions by value or reference.
- By value:
A copy of structure is passed, original remains unchanged.
- By reference:
Address is passed, changes affect original structure.
- Example:
- void display(struct Student s) {
-
printf("%d %s", s.id, s.name);
- }
- Calling: display(s1);.
- Useful for modular programming.
- Functions can process student,
employee, or product records.
- Helps in organizing large
applications.
- Efficient way to pass and
manipulate real-world data.
Thank
You
::
Any Query ::
Contact:
Ruparel Education Pvt. Ltd.
Mobile
No: 7600044051