Understanding One Dimensional Array in C Programming

What is Array?

Some programs may need to handle same type of different number of data having same characteristics. In such situation, it will be easier to handle such data in Array, where same name is shared for all data with different subscripts.

Array is a collection of similar data types with contiguous memory location; defined by same name. Each item in the memory location is known as element of an array. These elements are distinguished by array index. In an array, all the data items must be of same type and same storage class. Eg. either integer, floating point or characters.

single dimensional array index size

Array Index

Array Index

Each array element (individual array element) is denoted with a name followed by one or more subscripts (where each subscript must be non-negative integers within a pair of square bracket). These subscripts denote; on which memory location the array element are placed and how far from the first element of array, and are known as index of array. The index of first element is 0, the second element has an index of 1 and so on. The last element has an index of arraysize-1

Arrays in Memory

The amount of storage required to hold an array is directly related to its type and size.

Total size of array in bytes = sizeof(base type) × length of array

All arrays consist of contiguous memory locations

– the lowest address corresponds to the first element

– the highest address to the last element

Elements on array in contagious memory location

Elements on array in contagious memory location

The number of subscript depends on the dimensionality of the array. Eg. a[i] refers to an element of one dimensional array. Whereas b[i][j] refers to an array element of two dimensional array. In the same manner c[i][j][k] for three dimensional array.

Single/One Dimensional Array

The array which is used to represent and store data in a linear form is called as ‘single or one dimensional array.’

Syntax:

	<data-type> <array_name> [size];

Example:

	int a[3] = {2, 3, 5};
	char ch[20] = "TechnoExam" ;
	float stax[3] = {5003.23, 1940.32, 123.20} ;

Total Size (in Bytes):

	total size = length of array * size of data type

In above example, a is an array of type integer which has storage size of 3 elements. The total size would be 3 * 2 = 6 bytes.

Defining an array

Array is also defined in the same manner as ordinary variables, but it should also include the size of specification that shows the maximum size of elements in that array. The size must be defined with a constant ie. cannot be used any variable as subscript while defining an array. But it can be a symbolic constant while defining size of an array.

It is defined such as follows:

Storage_class data_type array[expression];
e.g.      int x [5];

float y[3][4];

char name [MAX];

where MAX is symbolic constant.

 

Initializing one dimensional Array

Although it is not possible to assign to all elements of an array at once using an assignment expression, it is possible to initialize some or all elements of an array when the array is defined. The syntax looks like this:

int a[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
static float x[6] = {0, 0.25, 0, –0.50, 0, 0};
char color[3] = {'R', 'E', 'D'};

The list of values, enclosed in braces {}, separated by commas, provides the initial values for successive elements of the array.

If there are fewer initializers than elements in the array, the remaining elements are automatically initialized to 0. For example,

        int a[10] = {0, 1, 2, 3, 4, 5, 6};

would initialize a[7], a[8], and a[9] to 0. When an array definition includes an initializer, the array dimension may be omitted, and the compiler will infer the dimension from the number of initializers. For example,

        int b[] = {10, 11, 12, 13, 14};

would declare, define, and initialize an array b of 5 elements (i.e. just as if you’d typed int b[5]). Only the dimension is omitted; the brackets [] remain to indicate that b is in fact an array.

In the case of arrays of char, the initializer may be a string constant:

        char s1[7] = "Hello,";
        char s2[10] = "there,";
        char s3[] = "world!";

As before, if the dimension is omitted, it is inferred from the size of the string initializer.

The array size can be omitted if you initialize the array elements

int digits[] = {1, 2, 3, 4, 5, 6};

– the size of digits is 6

char color[] = "RED";

– the size of color is 4

Accessing Elements of an Array

Individual elements of array can be pointed with the index of the array. Index of Array makes it easy to access elements of an array as the index of array starts from 0 to the nth term of Array element, as we discussed the last element index can be referred by [size-1]. Thus, array_name[2] refers to the 3rd element of the array. Now, to simply refer to the element of the array we can use increment counter inside loop, increasing the counter by value of 1.

Remember, it is good to start value of i with 0, as index of array starts from 0 and then increasing by 1.

Entering Data into an Array

So, you know now how to access elements of an Array. But, how would you enter data into array? Previously, we initialized array with static values. Here, we will be entering dynamic values or element now will be entered by users, not programmers. Again, for loop in c programming’ can be used with same incremental counter with initial value 0, but now use input statements:

for(i=0; i<SIZE; i++)

{

scanf(“%d”, &array[i]);

}

Reading Data from an Array

Now, after entering data/elements into array, you need to read data from array to print them out. Easy as it is, after knowing how to access the each elements on array using the index of array element. All you need to do is to access each element and print them out using printf’ statement as in:

for(i=0; i<SIZE; i++)

{

printf(“%d”, array[i]);

}

You can follow us on Twitter, add us to your circle on Google+ or like our Facebook page . We will be providing you with best online tutorial on Programming, Database, Data Structures, Engineering, Case Studies and Computer Fundamentals.