Welcome to the exciting world of C programming! Today, we're going to delve into one of the fundamental data structures - Arrays.
Arrays are a collection of variables of the same data type, stored at contiguous memory locations. They allow you to work with multiple items of the same type, making it easier to manage and manipulate data in your programs.
Let's understand arrays with an example:
int numbers[5]; // An array of 5 integer variablesIn the above example, numbers is an array that can store 5 integer values. Each variable in the array is referred to by its index, starting from 0.
To access an element in an array, you use its index. Here's how you can access the first and last elements of our numbers array:
numbers[0] = 10; // Assigning the first element
numbers[4] = 20; // Assigning the last element (5th element since indexing starts from 0)There are three types of arrays in C:
One-dimensional arrays: These are the basic arrays we've been discussing. They have one set of indices.
Multidimensional arrays: These are arrays with more than one set of indices. They are useful for representing tables, matrices, and other multi-dimensional data structures.
Dynamic arrays: These arrays can resize themselves, making them suitable for handling varying amounts of data.
int numbers[] = {1, 2, 3, 4, 5}; // Initializing an array with 5 integer valuesvoid printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
}Which of the following is the correct way to initialize an array of 5 integer variables?
Stay tuned for more C programming lessons! We'll dive deeper into arrays and other data structures in our upcoming lessons. Happy coding! 💻🎉