Skip to content

Commit 9b7f3de

Browse files
committed
[Edit] C: Arrays
1 parent 7102a94 commit 9b7f3de

File tree

1 file changed

+144
-23
lines changed

1 file changed

+144
-23
lines changed
Lines changed: 144 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,90 @@
11
---
22
Title: 'Arrays'
3-
Description: 'In the C language, an array is a data structure of a fixed length that stores a list of values.'
3+
Description: 'An array in C is a data structure that allows users to store a fixed-size sequence of elements of the same data type.'
44
Subjects:
55
- 'Code Foundations'
66
- 'Computer Science'
77
Tags:
88
- 'Arrays'
99
- 'Lists'
10+
- 'Values'
1011
- 'Variable Types'
1112
CatalogContent:
1213
- 'learn-c'
1314
- 'paths/computer-science'
1415
---
1516

16-
In the C language, an array is a list of values, with a fixed length.
17+
An **array** in C is a data structure that enables users to store a fixed-size sequence of elements of the same [data type](https://www.codecademy.com/resources/docs/c/data-types). Arrays are useful when there is a need to work with collections of data, such as lists of numbers, characters, or other values.
1718

18-
Being able to store multiple pieces of related information in the same structure is very useful when writing C programs.
19+
**Key Characteristics:**
1920

20-
## Declaring an Array
21+
- **Fixed Size**: Once declared, the size of the array cannot be changed.
22+
- **Homogeneous Elements**: All array elements must be of the same data type (e.g., `int`, `float`, etc.).
23+
- **Indexed Access**: Elements are accessed using zero-based indexing (the first element is at index `0`).
24+
- **Contiguous Memory Allocation**: Array items are stored in consecutive memory locations, which makes accessing them efficient.
25+
- **Efficient Iteration**: Arrays can be easily traversed using loops, allowing users to perform operations on each element.
26+
- **No Bounds Checking**: C does not perform automatic bounds checking, so accessing out-of-range elements may lead to undefined behavior.
2127

22-
The syntax for declaring an array is first specify the data type, then a descriptive array name, followed by square brackets surrounding the array's length (number of items):
28+
## Declaring and Initializing an Array
29+
30+
Here is the syntax for declaring an array in C:
2331

2432
```pseudo
2533
type name[length];
2634
```
2735

28-
To declare an `int` array named `grades` with a length of 6:
36+
In the syntax:
37+
38+
- `type`: The data type of the elements to be stored in the array.
39+
- `name`: The name of the array.
40+
- `length`: The length or the number of elements to be stored in the array.
41+
42+
This example declares an `int` array named `grades` with a length of `6`:
2943

3044
```c
31-
int grades[6]; // An array to hold six integers
45+
int grades[6];
3246
```
3347

34-
Alternatively, the length can be omitted and the array's initial values can be assigned to it instead. Values are assigned inside of the curly brackets and separated by commas.
48+
Though an array can be initialized after declaration, it is generally initialized during declaration. Here is the syntax for it:
49+
50+
```pseudo
51+
type name[length] = {value1, value2, ..., valueN};
52+
```
53+
54+
In the syntax:
55+
56+
- `value1, value2, ..., valueN`: The elements to be stored in the array.
57+
58+
This example declares and intializes the `grades` array with values:
3559

3660
```c
3761
int grades[] = {96, 90, 78, 84, 88, 92};
3862
```
3963
40-
## Accessing Values with Indexes
64+
> **Note:** When arrays are initalized during declaration, the length is generally omitted.
4165
42-
The values in arrays are accessed using their index, or their position in the array. They can either be assigned or used this way.
66+
## Accessing Array Elements
67+
68+
The values in arrays are accessed using their index or position in the array.
69+
70+
For example, here is an array:
4371
4472
```c
4573
char vowels[] = {'a', 'e', 'i', 'o', 'u'};
4674
```
4775

48-
Note that in C, an array's indexes start at 0 instead of 1:
76+
The index chart for the array will look like this:
4977

50-
```pseudo
78+
```plaintext
5179
+-------------------------+
5280
Value | 'a' 'e' 'i' 'o' 'u' |
5381
+-------------------------+
5482
Index | 0 1 2 3 4 |
5583
+-------------------------+
5684
```
5785

86+
> **Note:** In C, an array's indices start at `0` instead of `1`.
87+
5888
An element can be accessed by referring to the array name and the element's index number:
5989

6090
- `vowels[0]` will give the value `'a'`
@@ -63,34 +93,125 @@ An element can be accessed by referring to the array name and the element's inde
6393
- `vowels[3]` will give the value `'o'`
6494
- `vowels[4]` will give the value `'u'`
6595

96+
Here is an example:
97+
6698
```c
67-
char vowels[] = {'a', 'e', 'i', 'o', 'u'};
99+
#include <stdio.h>
100+
101+
int main() {
102+
char vowels[] = {'a', 'e', 'i', 'o', 'u'};
103+
104+
putchar(vowels[3]);
68105

69-
putchar(vowels[3]);
70-
// Output: o
106+
return 0;
107+
}
71108
```
72109

73-
## Example
110+
The output will be:
74111

75-
Creating an array that holds the snowfall measurements (in the nearest inch) from the past 7 days:
112+
```shell
113+
o
114+
```
115+
116+
## Modifying Array Elements
117+
118+
The items of an array can be modified by directly assigning values to specific indices:
76119

77120
```c
78121
#include <stdio.h>
79122

80123
int main() {
81-
int snowfall[] = {10, 13, 14, 11, 9, 8, 6};
82-
printf("%d\n", snowfall[0]);
124+
int numbers[3] = {1, 2, 3};
125+
126+
numbers[0] = 10;
83127

84-
snowfall[2] = 16; // Update an element
85-
printf("%d\n", snowfall[2]);
128+
for (int i = 0; i < 3; i++) {
129+
printf("%d\n", numbers[i]);
130+
}
86131

87132
return 0;
88133
}
89134
```
90135

91-
The output would be:
136+
Here is the output:
92137

93138
```shell
94139
10
95-
16
140+
2
141+
3
96142
```
143+
144+
## Iterating an Array
145+
146+
Arrays in C can be traversed using loops such as `for`, `while`, or `do-while`. The `for` [loop](https://www.codecademy.com/resources/docs/c/loops) is most commonly used for this purpose:
147+
148+
```c
149+
#include <stdio.h>
150+
151+
int main() {
152+
int numbers[] = {5, 10, 15, 20, 25};
153+
int i;
154+
155+
for (i = 0; i < 5; i++) {
156+
printf("%d\n", numbers[i]);
157+
}
158+
159+
return 0;
160+
}
161+
```
162+
163+
Here is the output:
164+
165+
```shell
166+
5
167+
10
168+
15
169+
20
170+
25
171+
```
172+
173+
## Example: Basic Array Operations
174+
175+
Here's a complete example demonstrating array declaration, initialization, element modification, and iteration:
176+
177+
```c
178+
#include <stdio.h>
179+
180+
int main() {
181+
int marks[5] = {80, 85, 90, 75, 95};
182+
183+
// Modify one element
184+
marks[2] = 100;
185+
186+
// Print all elements
187+
for (int i = 0; i < 5; i++) {
188+
printf("Student %d: %d marks\n", i + 1, marks[i]);
189+
}
190+
191+
return 0;
192+
}
193+
```
194+
195+
Here is the output:
196+
197+
```shell
198+
Student 1: 80 marks
199+
Student 2: 85 marks
200+
Student 3: 100 marks
201+
Student 4: 75 marks
202+
Student 5: 95 marks
203+
```
204+
205+
## Frequently Asked Questions
206+
207+
### 1. Can arrays in C store elements of different data types?
208+
209+
No. arrays in C are homogeneous, meaning all elements must be of the same data type.
210+
211+
### 2. Is it possible to change the size of an array after declaration?
212+
213+
No. The size of an array is fixed once declared. For dynamic sizes, consider using dynamic memory allocation with `malloc()`.
214+
215+
### 3. What happens if I access an element out of the array's bounds?
216+
217+
Accessing out-of-bounds elements in an array leads to undefined behavior and can cause unpredictable results or crashes.

0 commit comments

Comments
 (0)