Technical Training
C TutorialsC Programming - Arrays
Array is a collection of same type elements under the same variable identifier referenced by index number. Arrays are widely used within programming for different purposes such as sorting, searching and etc. Arrays allow you to store a group of data of a single type. Arrays are efficient and useful for performing operations . You can use them to store a set of high scores in a video game, a 2 dimensional map layout, or store the coordinates of a multi-dimensional matrix for linear algebra calculations.
Arrays are of two types single dimension array and multi-dimension array. Each of these array type can be of either static array or dynamic array. Static arrays have their sizes declared from the start and the size cannot be changed after declaration. Dynamic arrays that allow you to dynamically change their size at runtime, but they require more advanced techniques such as pointers and memory allocation.
It helps to visualize an array as a spreadsheet. A single dimension array is represented be a single column, whereas a multiple dimensional array would span out n columns by n rows. In this tutorial, you will learn how to declare, initialize and access single and multi dimensional arrays.
Single Dimension Arrays
Declaring Single Dimension Arrays
Arrays can be declared using any of the data types available in C. Array size must be declared using constant value before initialization. A single dimensional array will be useful for simple grouping of data that is relatively small in size. You can declare a single dimensional array as follows:
- <data type> array_name[size_of_array];
Say we want to store a group of 3 possible data information that correspond to a char value, we can declare it like this:
- char game_map[4];
Note: In C language the end of string is marked by the null character '�'. Hence to store a group of 3 possible string data. We declare the array as char game_map[4]; This applies for char type array.
One-dimensional string array containing 3 elements.
| Array Element | game_map[0] | game_map[1] | game_map[2] | game_map[3] |
| Data | S | R | D | � |
One-dimensional integer array containing 3 elements.
- int emp_code[3];
| Array Element | emp_code[0] | emp_code[1] | emp_code[2] |
| Data | 7 | 5 | 8 |
Initializing Single Dimension Arrays
Array can be initialized in two ways, initializing on declaration or initialized by assignment.
Initializing on Declaration
If you know the values you want in the array at declaration time, you can initialize an array as follows:
Syntax:
- <data type> array_name[size_of_array] = {element 1, element 2, ...};
Example:
- char game_map[3] = {'S', 'R', 'D'};
This line of code creates an array of 3 chars containing the values 'S', 'R ', and 'D'.
Initialized by Assignment
- char game_map[3];
- game_map[0] = 'S';
- game_map[1] = 'R';
- game_map[2] = 'D';
Accessing Single Dimension Array Elements
Arrays are 0-indexed, so the first array element is at index = 0, and the highest is size_of_array – 1. To access an array element at a given index you would use the following syntax:
- array_name[index_of_element];
To set an element of an array equal to a value you would write:
- array_name[index_of_element] = value;
To access one of the game_map element:
- game_map[0]; //value of 'S'
- game_map[1]; //value of ' R'
- game_map[2]; //value of 'D'
Trying to access a value outside the bounds of index 1 through size_of_array – 1, results in runtime errors. Your compiler will not complain, but your program will crash when it executes.
For example:
- game_map[3];
will lead to a runtime error and you program would crash because you tried to access a memory location outside the bound of the array, otherwise known as a segmentation fault.
C Tutorials
- C Programming - An Overview
- C Programming - Data Types : Part 1
- C Programming - Data Types : Part 2
- C Programming - Constants and Identifiers
- C Programming - Operators
- C Programming - Expressions
- C Programming - Managing Input and Output Operations
- C Programming - Decision Making - Branching
- C Programming - Decision Making - Looping
- C Programming - Arrays
- C Programming - Handling of Character String
- C Programming - Functions (Part-I)
- C Programming - Functions (Part-II)
- C Programming - Structures and Unions
- C Programming - Pointers
- C Programming - Dynamic Memory allocation
- C Programming - Linked Lists
- C Doubly Linked Lists
- C Circular Linked Lists
- C Programming - File management in C
- C Language - The Preprocessor
- Call by Value and Call by Reference
- Concept of Pixel in C Graphics
- TSR in C - An Introduction







