0% found this document useful (0 votes)
26 views5 pages

C Arrays: Definition, Initialization, and Usage

This document provides an overview of arrays in C, including definitions, declarations, initializations, and accessing elements. It explains one-dimensional and two-dimensional arrays, memory representation, and the use of arrays in functions. Additionally, it covers sorting techniques and the concept of multidimensional arrays.

Uploaded by

manjumittal2709
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views5 pages

C Arrays: Definition, Initialization, and Usage

This document provides an overview of arrays in C, including definitions, declarations, initializations, and accessing elements. It explains one-dimensional and two-dimensional arrays, memory representation, and the use of arrays in functions. Additionally, it covers sorting techniques and the concept of multidimensional arrays.

Uploaded by

manjumittal2709
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

UNIT – I: Arrays in C

1. What is an Array in C? (Defini on)


An array is a collec on of similar data type elements stored in con guous
memory loca ons. It is used to store mul ple values using a single
variable name.
Example: To store marks of 5 students, instead of wri ng:
int m1, m2, m3, m4, m5;
We write:
int marks[5];

2. Declara on and Ini aliza on of One-Dimensional Array


Declara on syntax:
data_type array_name[size];
Example:
int arr[5];
Sta c Ini aliza on (at the me of declara on):
int arr[5] = {10, 20, 30, 40, 50};
You can also omit the size:
int arr[] = {10, 20, 30}; // Compiler sets size to 3
Run me Ini aliza on (taking input using loop):
int arr[5];
for(int i = 0; i < 5; i++) {
scanf("%d", &arr[i]);
}

3. Accessing Array Elements


Array elements are accessed using indexing, and the index starts from 0.
arr[0] = 100; // Assign value
prin ("%d", arr[0]); // Access value

4. Displaying Array Elements


To display elements, we use a loop:
for(int i = 0; i < 5; i++) {
prin ("%d ", arr[i]);
}

5. Sor ng Array (Bubble Sort in Ascending Order)


This code sorts array elements from smallest to largest:
int temp;
for(int i = 0; i < size - 1; i++) {
for(int j = 0; j < size - i - 1; j++) {
if(arr[j] > arr[j+1]) {
// swap
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}}
6. Arrays and Func ons
Passing Array to a Func on:
void printArray(int arr[], int size) {
for(int i = 0; i < size; i++) {
prin ("%d ", arr[i]);
}
}
Calling Func on:
int a[5] = {10, 20, 30, 40, 50};
printArray(a, 5);
In C, arrays are passed to func ons by reference (actually passing the
base address).

7. Two-Dimensional Arrays
Used to store data in a table (matrix) format – rows and columns.
Declara on:
int arr[2][3];
Ini aliza on:
int arr[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
Access and Display:
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 3; j++) {
prin ("%d ", arr[i][j]);
}
prin ("\n");
}

8. Memory Representa on of 2D Array


Row-Major Order (used by C):
All rows are stored one a er another in memory.
int arr[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
Stored as: 1, 2, 3, 4, 5, 6

Column-Major Order (used in some other languages like Fortran):


Columns are stored one a er another.
Stored as: 1, 4, 2, 5, 3, 6

9. Mul dimensional Arrays


An array with more than 2 dimensions is called a mul dimensional array.
Declara on:
int arr[2][3][4]; // 3D array
Ini aliza on:
int arr[1][2][2] = {{{1, 2}, {3, 4}}};
Mul dimensional arrays are used in scien fic calcula ons, image
processing, etc.

You might also like