Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C program to demonstrate usage of variable-length arrays
Variable-length arrays (VLAs) in C allow you to create arrays whose size is determined at runtime rather than compile time. This feature was introduced in C99 and provides dynamic array allocation on the stack. In this example, we'll demonstrate a library system that uses VLAs to manage books on shelves with three operations: adding books, querying book pages, and counting books per shelf.
Syntax
data_type array_name[variable_size]; // Where variable_size is determined at runtime
Library Management System
The system supports three commands −
- Command 1: Insert a book with y pages at shelf x
- Command 2: Print the page number of the y-th book at shelf x
- Command 3: Print the number of books on shelf x
Commands are given as a 2D array in format {command_type, x, y}. If no y value exists, it defaults to 0.
Example
Here's the implementation using variable-length arrays for dynamic memory management −
#include <stdio.h>
#include <stdlib.h>
void solve(int shelves, int queries, int query_array[][3]) {
int book_count[shelves]; // VLA for book counts per shelf
int* pages[shelves]; // Array of pointers for pages
/* Initialize shelves */
for(int i = 0; i < shelves; i++) {
book_count[i] = 0;
pages[i] = NULL;
}
/* Process each query */
for(int i = 0; i < queries; i++) {
int command = query_array[i][0];
int shelf = query_array[i][1];
int value = query_array[i][2];
if(command == 1) {
/* Add book with 'value' pages to 'shelf' */
book_count[shelf]++;
pages[shelf] = realloc(pages[shelf], book_count[shelf] * sizeof(int));
pages[shelf][book_count[shelf] - 1] = value;
}
else if(command == 2) {
/* Print pages of book at position 'value' on 'shelf' */
printf("%d
", pages[shelf][value]);
}
else {
/* Print number of books on 'shelf' */
printf("%d
", book_count[shelf]);
}
}
/* Free allocated memory */
for(int i = 0; i < shelves; i++) {
if(pages[i] != NULL) {
free(pages[i]);
}
}
}
int main() {
int input_queries[][3] = {{1, 3, 23}, {1, 4, 128}, {2, 3, 0}, {3, 4, 0}};
int num_shelves = 5;
int num_queries = 4;
printf("Library Management System Output:
");
solve(num_shelves, num_queries, input_queries);
return 0;
}
Output
Library Management System Output: 23 1
How It Works
The program processes the following operations −
- Command {1, 3, 23}: Adds a book with 23 pages to shelf 3
- Command {1, 4, 128}: Adds a book with 128 pages to shelf 4
- Command {2, 3, 0}: Prints pages of first book (index 0) on shelf 3 ? outputs 23
- Command {3, 4, 0}: Prints number of books on shelf 4 ? outputs 1
Key Points
- Variable-length arrays like
int book_count[shelves]are allocated on the stack - VLAs provide automatic memory management without explicit malloc/free
- The array size must be known at declaration time but can be a runtime variable
- Always free dynamically allocated memory to prevent memory leaks
Conclusion
Variable-length arrays in C provide a convenient way to create stack-allocated arrays with runtime-determined sizes. This example demonstrates their practical use in building dynamic data structures for real-world applications like library management systems.
