# Using Arrays C Language with Example

In a single sentence, an Array is a container of the homogeneous data type. that can store a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.

Instead of declaring individual variables, such as num0, num1, …, and num99, you declare one array variable such as numbers and use num[0], num[1], and …, num[99] to represent individual variables. A specific element in an array is accessed by an index.

All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.

## Subscripts and pointer arithmetic:

array[subscript] equivalent to *(array + (subscript))

Strange but true. Given earlier declaration of m, the expression 2[m] is legal!. Not only that it’s equivalent to :

*(2+m)

*(m+2)

m[2]

## Initializing arrays:

By default, regular arrays of local scope (for example, those declared within a function) are left uninitialized. This means that none of its elements are set to any particular value; their contents are undetermined at the point the array is declared. A user can put element and execute during program execution.

But the elements in an array can be explicitly initialized to specific values when it is declared, by enclosing those initial values in braces {}. For example:

` int num[5]={10,5,17,4,8};`

Array Pocket index starts with 0 and last pocket index is array element -1. Assume that array element is n then  last pocket index is n-1.

The number of values between braces `{}` should not greater than the number of elements in the array. If its happens then value overrides from the first element of this array.  Assume that you have an array num[5]  with 5 elements. After that, you initialized  it with 7 values. num[5]={10,5,17,4,8,15,12}

` int num[5]={10,5,17,4,8,15,12};`

First of all its grab array pockets as chronological order.

But you still have two more values remain to be placed. The remain two values override the first and second pocket of the array. Finally its looks like bellow:

If you initialized less element than declared size of the array. It’s ok the remaining pocket has 0 value by default.

` int num[5]={10,5,17};`

The initializer can even have no values, just the braces:

` int num [5] = { };`

When an initialization of values is provided for an array, C/C++ allows the possibility of leaving the square brackets empty`[]`. In this case, the compiler will assume automatically a size for the array that matches the number of values included between the braces `{}`:

` int num[]={10,5,17,4,8};`

## Accessing the values of an array:

The values of any of the elements in an array can be accessed just like the value of a regular variable of the same type. The syntax is:

`name[index]`
Following the previous examples in which num had 5 elements and each of those elements was of type `int`, the name which can be used to refer to each element is the following:

For example considering the following code segment. You are able to access its values from this array as follows:

`  int num[5]={10,5,17,4,8};`

```x=num[2];      // The value of x  is equal to 17

x=num[4];      // The value of x is equal to 8```
``` scanf("%d",&num[2]);
/* statement to insert value in the third element of array num[]. */

scanf("%d",&num[i]);
/* you can use a variable instead of number to insert value, in this case (i+1)th element of array num[]. Because array use zero(0) based index, the first element of array is num[0], second is num[1], ith is num[i-1] and (i+1)th is num[i]. */

printf("%d",num[0]);
/* statement to print first element of an array. */

printf("%d",num[i]);
/* statement to print (i+1)th element of an array. */```

## Multidimensional arrays:

Multidimensional arrays can be described as “arrays of arrays”. It can be two dimensional, three dimensional and even more in deep. However, arrays more than three levels deep are hard to manage for most people. Generally, two-dimensional array we can imagine as a two-dimensional table and three-dimensional array can imagine as a cube of a two-dimensional table.

Declaring multidimensional array :

` int num[5][5];`

` int num[3][5][4];`

See:

In next tutorial, we learn execution of array its applications. it is very grateful to me if this tutorial is helpful for you. Thank you.

The following two tabs change content below.

#### Subroto Mondal

Chief Coordinator HR&CR
I like Programming and New Technologies. And work with Linux.

## 6 thoughts on “Using Arrays C Language with Example”

1. Fantastic items from you, man. I’ve take into account your stuff prior to and you are
just too fantastic. I actually like what you have received right here, really like
what you’re saying and the best way in which you assert it.
You’re making it entertaining and you still care for
to keep it smart. I cant wait to learn far more from you.
That is actually a wonderful website.

2. Simply wish to say your article is as amazing. The clarity in your post is just excellent and i can assume you
are an expert on this subject. Well with your permission allow me to grab your feed to keep updated
with forthcoming post. Thanks a million and please keep up the rewarding work.

3. Woh I like your posts, saved to my bookmarks!

4. Heya i am for thhe primary time here. I found this board and I find It truly helpful & it helped me
out much. I am hoping to provide one thing again annd help others like you aided me.

5. I quite like looking through a post that will make people think.

Also, many thanks for allowing for me to comment!

This site uses Akismet to reduce spam. Learn how your comment data is processed.