Search⌘ K

Handling Data Using Arrays

Explore how to manage data using multiple arrays in C and understand the disadvantages such as difficulty in handling many arrays and losing the natural relationship between data fields. This lesson helps you recognize why arrays may be impractical for complex data and prepares you for more efficient data structures.

Storing data using multiple arrays

The program given below shows how the following data can be stored using multiple arrays.

Press the RUN button and see the output!

C
# include <stdio.h>
int main( )
{
// Create char array to store name of employees
char names[ ] = { 'A', 'X', 'Y', 'Z', '\0' } ;
// Create int array to store age of employees
int ages[ ] = { 23, 27, 28, 22 } ;
// Create float array to store salary of employees
float salaries[ ] = { 4000.50, 5000.00, 6000.75, 5600.55 } ;
int i ;
// Traverse arrays and print its elements to the console
for ( i = 0 ; i <= 3 ; i++ )
printf ( "%c %d %f\n", names[ i ], ages[ i ], salaries[ i ] ) ;
}

...