0% found this document useful (0 votes)
8 views8 pages

Arrays in C

Uploaded by

dark.sci.magic
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)
8 views8 pages

Arrays in C

Uploaded by

dark.sci.magic
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
You are on page 1/ 8

Detailed Notes on Arrays in C

### Definition of Arrays


An array is a data structure that can store a fixed-size sequential
collection of elements of the same data type. It is a collection of
variables that are accessed using indices.

---

### Types of Arrays


1. **One-Dimensional Array (1D Array)**: A linear collection of
elements.
2. **Two-Dimensional Array (2D Array)**: A collection of elements
arranged in rows and columns (like a matrix).
3. **Multi-Dimensional Array**: Arrays with more than two
dimensions.

---

### Key Topics in Arrays

#### 1. Declaration and Initialization


- **Syntax**:
```c
data_type array_name[size];
```
Example:
```c
int arr[5]; // Declaration of an array of size 5
```

- **Initialization**:
```c
data_type array_name[size] = {value1, value2, ..., valueN};
```
Example:
```c
int arr[5] = {1, 2, 3, 4, 5};
```

#### 2. Accessing Array Elements


- Use the index to access or modify elements.
- **Syntax**:
```c
array_name[index];
```
Example:
```c
int arr[5] = {10, 20, 30, 40, 50};
printf("%d", arr[2]); // Output: 30
arr[3] = 100; // Modify the value at index 3
```
#### 3. 1D Arrays
- A simple linear collection of elements.
- **Example**:
```c
#include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50};
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
return 0;
}
```

#### 4. 2D Arrays
- Represents a table of rows and columns.
- **Syntax**:
```c
data_type array_name[rows][columns];
```
Example:
```c
int arr[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
```

#### 5. Multi-Dimensional Arrays


- Arrays with dimensions more than two.
- **Syntax**:
```c
data_type array_name[size1][size2][size3]...;
```
Example:
```c
int arr[2][2][2] = {
{{1, 2}, {3, 4}},
{{5, 6}, {7, 8}}
};
```

#### 6. Array of Pointers


- An array storing addresses of other variables or arrays.
- **Example**:
```c
int a = 10, b = 20;
int *arr[2];
arr[0] = &a;
arr[1] = &b;
printf("%d %d", *arr[0], *arr[1]); // Output: 10 20
```
#### 7. Dynamic Arrays
- Arrays whose size can be modified during runtime using memory
allocation.
- **Example**:
```c
#include <stdlib.h>
#include <stdio.h>
int main() {
int *arr = (int *)malloc(5 * sizeof(int));
for (int i = 0; i < 5; i++) {
arr[i] = i + 1;
}
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
free(arr);
return 0;
}
```

#### 8. Passing Arrays to Functions


- Arrays can be passed to functions as pointers.
- **Example**:
```c
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
}
int main() {
int arr[5] = {1, 2, 3, 4, 5};
printArray(arr, 5);
return 0;
}
```

#### 9. Searching in Arrays


- **Linear Search**:
```c
int search(int arr[], int size, int key) {
for (int i = 0; i < size; i++) {
if (arr[i] == key)
return i;
}
return -1;
}
```
- **Binary Search** (for sorted arrays):
```c
int binarySearch(int arr[], int size, int key) {
int low = 0, high = size - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (arr[mid] == key)
return mid;
else if (arr[mid] < key)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
```

#### 10. Sorting Arrays


- **Bubble Sort**:
```c
void bubbleSort(int arr[], int size) {
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
```

---
### Common Mistakes
1. **Out of Bounds Access**:
- Accessing indices beyond the size of the array leads to undefined
behavior.
2. **Uninitialized Arrays**:
- Using an uninitialized array can result in garbage values.
3. **Passing Arrays Incorrectly to Functions**:
- Always pass the correct size to avoid segmentation faults.

You might also like