Technical Training
C TutorialsArray 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.
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:
Say we want to store a group of 3 possible data information that correspond to a char value, we can declare it like this:
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.
| Array Element | emp_code[0] | emp_code[1] | emp_code[2] |
| Data | 7 | 5 | 8 |
Array can be initialized in two ways, initializing on declaration or initialized by assignment.
If you know the values you want in the array at declaration time, you can initialize an array as follows:
This line of code creates an array of 3 chars containing the values 'S', 'R ', and 'D'.
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:
To set an element of an array equal to a value you would write:
To access one of the game_map element:
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.
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
H I D E