
Arrays in C
What are Arrays in C?
- Arrays are a fundamental data structure in C, allowing you to store multiple elements of the same data type in a next or together in memory space.
- This guide will cover one-dimensional (1D) and two-dimensional (2D) arrays.
- As well as essential concepts like strings, pointers, array-pointer relationships, pointer arithmetic, dynamic memory allocation, and more.
Array Declaration
- In C, you declare an array by specifying its data type, followed by the array name and the number of elements it can hold within square brackets.
- The general syntax for declaring an array is as follows:
Loading…
Here's what each part of the declaration means:
- data_type: This is the data type of the elements that the array will hold. It can be any valid C data type, such as int, char, float, etc.
- array_name: This is the name you give to the array, which allows you to refer to and manipulate the array later in your code.
- array_size: The number of elements the array can hold. This size must be an integer value, and it defines how many elements the array can store.
For example, if you want to declare an integer array that can store 5 elements, you would do it like this:
Loading…
This declares an integer array named myArray with the capacity to hold 5 integer values.
You can later initialize and access these elements using their indices, which start from 0.
Loading…
In this example, we've declared three different arrays:
- numbers is an integer array that can hold 5 integer values.
- vowels is a character array that can hold 5 characters (like letters of the alphabet).
- prices is a double array that can hold 10 double-precision floating-point values.
These arrays are declared but not yet initialized with specific values.
You can later assign values to the elements within these arrays as needed in your code.
Types of Arrays in C
- One-Dimensional (1D) Arrays
- Two-Dimensional (2D) Arrays
1. One-Dimensional (1D) Arrays
Declaration
- To declare a 1D array, you specify the data type and the array's name, followed by the number of elements in square brackets.
Loading…
- You can initialize the array during declaration or later using a loop or explicitly specifying values.
2. Two-Dimensional (2D) Arrays
Declaration
- A 2D array is like a table with rows and columns.
- You specify the data type, array name, and dimensions in square brackets.
Loading…
Initialization
You can initialize a 2D array similarly to 1D arrays, with nested loops for each row and column.
Loading…
3. Strings
Declaration
Strings in C are essentially arrays of characters.
Loading…
4. Pointers
Declaration
- Pointers are variables that store memory addresses.
- You declare a pointer with an asterisk (*) followed by the data type.
Loading…
Relationship between Arrays and Pointers
- Arrays and pointers are closely related.
- An array name can be used as a pointer to its first element.
Loading…
5. Pointer Arithmetic
Pointer arithmetic allows you to move through the elements of an array.
Loading…
6. Dynamic Memory Allocation
- Dynamic memory allocation allows you to allocate memory at runtime.
- The functions malloc, calloc, and realloc are used to allocate memory, and free is used to deallocate it.
Loading…
7. Pointer to Arrays
You can use pointers to access elements of arrays, which is closely related to the relationship between arrays and pointers, as shown earlier.
Loading…
In this example, we declare an integer array myArray, and then we create a pointer ptr that points to the first element of the array. We use the pointer to access individual elements.
8. Array of Pointers
An array of pointers allows you to store multiple pointers, each pointing to a different data structure, such as an array or string.
Loading…
Here, we declare an array of integer pointers, ptrArray, with space for three pointers. We then store the addresses of different integer variables in the elements of the array and use the array to access and print these values.
9. Pointers to Functions
Pointers to functions allow you to call functions dynamically. You can define a function pointer and use it to call different functions at runtime.
Loading…
In this code, we declare a pointer to a function that takes two integers and returns an integer. We then point this function pointer to different functions (add and subtract) and use it to call these functions dynamically.
10. Array of Pointers to Function
You can create arrays of pointers to functions, which is useful for implementing function tables and dynamically selecting functions to call based on program logic.
Loading…