Welcome to our deep dive into C 1D Arrays! 🎯
In this lesson, we'll learn about one-dimensional arrays, which are a collection of elements of the same type stored in contiguous memory locations. Let's start with the basics!
An array is a special variable that can store multiple values of the same type. It's like having a box with multiple compartments, where each compartment stores a single item.
To create an array in C, you first declare a variable of the array type, and then specify its size. Here's an example:
int numbers[5];In this example, numbers is an array that can store 5 integers.
To access an element in an array, you use its index. The first element has an index of 0, the second has an index of 1, and so on. Here's an example:
int numbers[5] = {1, 2, 3, 4, 5};
numbers[0] // Output: 1
numbers[1] // Output: 2
numbers[4] // Output: 5Just like accessing, you can modify array elements using their index.
int numbers[5] = {1, 2, 3, 4, 5};
numbers[2] = 10;
numbers[0] // Output: 1
numbers[2] // Output: 10When you create an array, you specify its size. This size cannot be changed later. Here's an example:
int numbers[5]; // This array has a size of 5You can initialize an array with values when you create it.
int numbers[5] = {1, 2, 3, 4, 5};In this section, we'll cover more advanced array concepts such as multidimensional arrays, dynamic memory allocation, and array functions.
What is an array in C?
How do you access an element in an array in C?