C 1D Arrays (One-Dimensional) 🚀

beginner
21 min

C 1D Arrays (One-Dimensional) 🚀

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!

Table of Contents

  1. Introduction to Arrays
  2. Creating Arrays
  3. Accessing Array Elements
  4. Modifying Array Elements
  5. Array Sizes
  6. Initializing Arrays
  7. Advanced Array Concepts

1. Introduction to Arrays 📝

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.

2. Creating Arrays 💡

To create an array in C, you first declare a variable of the array type, and then specify its size. Here's an example:

c
int numbers[5];

In this example, numbers is an array that can store 5 integers.

3. Accessing Array Elements 📝

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:

c
int numbers[5] = {1, 2, 3, 4, 5}; numbers[0] // Output: 1 numbers[1] // Output: 2 numbers[4] // Output: 5

4. Modifying Array Elements 💡

Just like accessing, you can modify array elements using their index.

c
int numbers[5] = {1, 2, 3, 4, 5}; numbers[2] = 10; numbers[0] // Output: 1 numbers[2] // Output: 10

5. Array Sizes 📝

When you create an array, you specify its size. This size cannot be changed later. Here's an example:

c
int numbers[5]; // This array has a size of 5

6. Initializing Arrays 💡

You can initialize an array with values when you create it.

c
int numbers[5] = {1, 2, 3, 4, 5};

7. Advanced Array Concepts 📝

In this section, we'll cover more advanced array concepts such as multidimensional arrays, dynamic memory allocation, and array functions.

Quiz 📝

Quick Quiz
Question 1 of 1

What is an array in C?

Quick Quiz
Question 1 of 1

How do you access an element in an array in C?