- What is an array in C programming
- What is one dimensional (1D) array
- How to declare and initialize 1D arrays
- How to access elements using index
- Memory representation of 1D arrays
- Real-world examples and use cases
What is an Array in C Language?
In C programming, an array is a collection of similar elements or data items of the same type stored at contiguous memory locations.
Arrays are used to organize the same type of data. In C, arrays are a derived data type that can store primitive data type values such as int, float, char, double, etc.
Example: If we want to store the marks obtained by a student in 5 different subjects, instead of creating 5 separate variables, we can use a single array to store all the marks.
💡 Key Concept: Arrays store values in contiguous memory locations, meaning the elements are stored one after another in memory. This allows efficient access using index numbers.
What is One Dimensional (1D) Array?
A One Dimensional (1D) Array in C is a variable that stores a group of values of the same data type in contiguous memory locations. It has only one dimension — a single row of elements.
Examples of 1D Arrays:
- Marks: 60, 70, 99, 83, 12, 56, 73, 45, 65, 44
- Student Roll Numbers: 11, 34, 12, 33, 12, 15, 14, 17, 18, 19
- Employee Salaries: 85000, 45600.54, 87098.45, 23445, 888767.98, 4556.78
📌 Note: A 1D array is like a single row in a spreadsheet where each cell stores one value, and you can access each cell using its position (index).
Array Declaration in C
Before using an array in C, you must declare it like any other variable. The declaration tells the compiler about:
- Data Type: The type of elements (int, char, float, etc.)
- Array Name: The name of the array variable
- Size: The number of elements the array can hold
Syntax of Array Declaration:
Example:
float salary[10]; // Array to store 10 floating-point values
char name[20]; // Array to store 20 characters
Array Initialization
You can initialize an array at the time of declaration or later in the program.
1. Compile-Time Initialization:
int marks[] = {60, 70, 99, 83, 12}; // Size auto-calculated
2. Run-Time Initialization:
for(int i = 0; i < 5; i++) {
printf("Enter marks: ");
scanf("%d", &marks[i]);
}
Accessing Array Elements
Array elements are accessed using index numbers. In C, array indices start from 0.
| Array | Index 0 | Index 1 | Index 2 | Index 3 | Index 4 |
|---|---|---|---|---|---|
| marks[5] | marks[0] = 60 | marks[1] = 70 | marks[2] = 99 | marks[3] = 83 | marks[4] = 12 |
Example:
int x = marks[2]; // Access third element (value 99)
Memory Representation of 1D Array
When an array is declared, the compiler allocates a contiguous block of memory to store all elements.
Example: int marks[5];
- Base Address: Address of the first element (marks[0])
- Memory Size: Each int occupies 2 bytes (16-bit) or 4 bytes (32-bit/64-bit)
- Total Memory: size × number_of_elements
Address at 1 index is : 1002 (or 1004)
Address at 2 index is : 1004 (or 1008)
Address at 3 index is : 1006 (or 1012)
Address at 4 index is : 1008 (or 1016)
📝 Note: The address of an array is the address of its first element. The memory occupied depends on the data type and system architecture.
Real-World Examples & Use Cases
1. Matrix Operations: Arrays are used extensively in matrix calculations and linear algebra.
2. Database Management: RDBMS systems use arrays to store and manage data.
3. Lookup Tables: Arrays can be used to create efficient lookup tables.
4. Sorting and Searching: Arrays are fundamental in sorting and searching algorithms.
5. Data Structures: Arrays are used to implement stacks, queues, and graphs.
6. Data Mining: Arrays represent large datasets for analysis.
7. Robotics: Arrays represent object positions in 3D space.
💡 Versatility: Arrays can store integers, characters, floating-point numbers, pointers, and even complex data structures like structures and objects.
Advantages & Disadvantages of Arrays
✅ Advantages
- Easy to access elements using index position
- Values can be searched very easily
- Stores multiple data items of the same type using a single name
- Improves performance and code efficiency
- Enables iteration through elements using loops
❌ Disadvantages
- Array is static in nature (fixed size)
- Must know the number of elements in advance
- Insertion and deletion is difficult
- Cannot store elements of different data types
- Memory wastage if not fully used
Frequently Asked Questions
1. What is a one-dimensional array in C?
A one-dimensional array is a collection of elements of the same data type stored in contiguous memory locations. It has only one dimension and can be accessed using a single index.
2. What is the syntax to declare a 1D array?
data_type array_name[size];
Example: int marks[10];
3. What is array index in C?
Array index is an integer that identifies the position of an element within the array. Index starts from 0 for the first element and goes up to size-1 for the last element.
4. How are arrays stored in memory?
Arrays are stored in contiguous memory locations. The address of the first element is the base address, and subsequent elements are stored at consecutive memory addresses.
5. What is the difference between compile-time and run-time initialization?
Compile-time: Values are assigned during compilation.
Run-time: Values are assigned during program execution using input functions like scanf().
💡 Tip: Always remember that array index starts from 0 in C, not 1. This is a common mistake beginners make.