

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 3
click for more info
Not enough gems
Cost: 6 gems
1: Memory
incomplete
2: What Is an Address?
incomplete
3: Virtual Memory
incomplete
4: Pointers
incomplete
5: Why Pointers?
incomplete
6: Pointer Basics
incomplete
7: Pointers to Structs
incomplete
8: C Arrays
incomplete
9: Arrays As Pointers in C
incomplete
10: Multibyte Arrays
incomplete
11: Array Casting
incomplete
12: Pointer Size
incomplete
13: Arrays Decay to Pointers
incomplete
14: C Strings
incomplete
15: C String Library
incomplete
16: Forward Declaration
incomplete
17: Mutual Structs
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
If you're used to Lists in Python, Arrays in C are similar, but a bit lower level.
An array is a fixed-size, ordered collection of elements. Like Python lists, they are indexed by integers, starting at zero. Unlike Python lists, they can only hold elements of the same type. They are stored in contiguous memory, like structs.
int numbers[5] = {1, 2, 3, 4, 5};
In C, there is no for x in list: syntax. Instead, you must iterate over them using a for loop with an index (or some other conditional loop)
#include <stdio.h>
int main() {
int numbers[5] = {1, 2, 3, 4, 5};
// Iterate and print each element
for (int i = 0; i < 5; i++) {
printf("%d ", numbers[i]);
}
printf("\n");
return 0;
}
Output:
1 2 3 4 5
The syntax for updating values in an array is the same as how you access them:
arr[index] = value
Using our numbers example:
#include <stdio.h>
int main() {
int numbers[5] = {1, 2, 3, 4, 5};
// Update some values
numbers[1] = 20;
numbers[3] = 40;
// Print updated array
for (int i = 0; i < 5; i++) {
printf("%d ", numbers[i]);
}
printf("\n");
return 0;
}
Output:
1 20 3 40 5
Complete the update_file function. The filedata array is a large 200-integer array representing a Sneklang source file. Each integer in the array represents a special piece of data.
1 is the number of lines2 is the filetype199 is always 0Update the function so that it:
By modifying the array within your function, you're changing the values of the original array, not just a copy. More on that later.