## Jul 17, 2014

### C Programming #39: Array - single dimension

Following article will discuss single dimension arrays in C language. Let me deal with the concept of array from different angle by developing the necessity of by giving example.

Program to find the average of 10 intergers :-

#include <stdio.h>

int main() {
int n0 = 10;
int n1 = 3;
int n2 = 3;
int n3 = 70;
int n4 = 22;
int n5 = 3;
int n6 = 20;
int n7 = 50;
int n8 = 1;
int n9 = 101;

int sum;
float avg;

sum = n0 + n1 + n2 + n3 + n4 + \
n5 + n6 + n7 + n8 + n9;

avg = sum / 10.0;

printf("Avg of 10 number is %f\n", avg);

return 0;

}


Output of the above program is

Avg of 10 number is 28.299999


Digressing from the topic, Note

• Sum calculation expression is divided to multiple line. Here backslash tells compiler that next line is part of same line.
• Note while calculating avg we are using 10 as double, to understand this read C Programming #07 Operators - Arithmetic

Coming back to program. You might tell that program compiles and also helps in finding the average. If you are yet not seeing the issue, let me make it bigger. How about writing program that find average of 1000 numbers ? Issue with above program is variables, it is just not manageable. This is where Array will come to rescue of programmer.

Syntax:

data_type array_name[array_size] = {init_val1, init_val2, ..., init_valn};

Lets see how to use it by re-writing Program.

#include <stdio.h>

int main() {
int n[10] = { 10, 3, 3, 70, 22, \
3, 20, 50, 1, 101};
int sum = 0;
int counter;
float avg;

for(counter = 0; counter < 10; counter++) {
sum = sum + n[counter];
}

avg = sum / 10.0;

printf("Avg of 10 number is %f\n", avg);

return 0;

}


10, 3, 70, 22, 3, 20, 50, 1, 101 are 10 initial value that will occupy space in n[0], n[1], n[2], n[3], n[4], n[5], n[6], n[7], n[8], n[9] respectively. In definition n[10], here 10 is the size of array. While in expression sum = sum + n[counter] here counter is the index of array. If the array size is 10 then the valid index that can be used are 0 to 9.  Always note that in C array index starts at 0, not at 1. This is sometimes common confusion.

Lets see how array is allocated in memory, by taking another example.

#include <stdio.h>

int main()
{
int n[5] = { 11, 13, 32, 43, 22 };

printf("Size of n    is %d\n", sizeof(n));
printf("Size of n[0] is %d\n", sizeof(n[0]));

printf("Address of n[0] is %p\n", &n[0]);
printf("Address of n[1] is %p\n", &n[1]);
printf("Address of n[2] is %p\n", &n[2]);
printf("Address of n[3] is %p\n", &n[3]);
printf("Address of n[4] is %p\n", &n[4]);

printf("Address if n is %p\n", &n);
printf("Address Value of n is %p\n", n);

return 0;
} 

Output of above program is

Size of n    is 20
Size of n[0] is 4
Address Value of n is 0x7fff7b95f360


Size of each element of array is 4 bytes (since integer), there are 5 elements in array. Hence size of array is 20 bytes (4*5). Now coming to address n[0], n[1], n[2], n[3], n[4] all at contiguously allocated.

Following is memory diagram depicting how the array elements are allocated.

Hence compiler makes sure that when allocating array all the elements are consecutively placed. In similar way we have array of character, float, doubt, typedef.

Array of character (These are also called strings [with some modification] which will be covered later)

char a[3] = {'a', 'b', 'c' };

Array of float
float f[4] = {1.1, 2.1, 3.2, 4.7};

Array of typedef
typedef int roll_number_t;
roll_number_t r[4] = {101, 102, 103, 104};

Un-initialized array
We can whole array un-initialized if we don't want initial value in them.
Example is
int a[100];
Here a is array of 100 integers. The value in each element is unspecified (means it can be any thing).

Partially initialized array

int a[5] = {5,8};

Here a is array of 5 integers, only two initial values are given 5 and 8. These initial value will be assigned to a[0] and a[1] and all uninitialized elements will be initialized to 0 by compiler implicitly.
Hence final value will
a[0] is 5
a[1] is 8
a[2] is 0
a[3] is 0
a[4] is 0
Note we cannot initialize middle of array, we can only have initialized array partially only from starting of array.

Array size omitted.

Array size can be omitted if we are initializing elements

e.g

int a[] = {1, 2, 5, 9};

Here the array_size is not specified, C compiler knows from initial value that a needs to be array of 4 integers.

While the following construct is
float f[];  - is invalid statement.

C does not allow mixed type array, say we want to form a array of 10 elements in which first 3 element is interger, next 3 elements is float and last 4 elements in characters. Such constructs in C are not at all allowed in C.

Array Definition:

An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by same name with index notation.