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...
Key Architecture Takeaways
- ›Point 1: Core computer science and engineering computing concept: Using Arrays C Language with Example.
- ›Point 2: Detailed trace of memory representation, algorithmic complexity, and control structures.
- ›Point 3: Includes portable ISO C (C17/C23) code examples verified with modern GCC and Clang.
Compiler & Toolchain Modernization
While legacy educational environments used 16-bit DOSBox Turbo C++, all code presented here adheres to standard ANSI/ISO C standards. You can compile these programs cleanly on modern Linux, Windows (MSYS2/WSL), or macOS using:
> gcc -Wall -Wextra -O2 program.c -o program
> ./program
> ```
---
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.
## Declaring Array:
[](http://technhit.in/wp-content/uploads/2016/08/array-declearation.png)
## 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:c int num[5]={10,5,17,4,8};
[](http://technhit.in/wp-content/uploads/2016/08/array-exam5.png)
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}*c int num[5]={10,5,17,4,8,15,12};
First of all its grab array pockets as chronological order.
[](http://technhit.in/wp-content/uploads/2016/08/array-exam6.png)
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:
[](http://technhit.in/wp-content/uploads/2016/08/array-exam7.png)
If you initialized less element than declared size of the array. It's ok the remaining pocket has *0* value by default.c int num[5]={10,5,17};
[](http://technhit.in/wp-content/uploads/2016/08/array-exam8.png)
The initializer can even have no values, just the braces:c int num [5] = { };
[](http://technhit.in/wp-content/uploads/2016/08/array-exam9.png)
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 `{}`:c int num[]={10,5,17,4,8};
[](http://technhit.in/wp-content/uploads/2016/08/array-exam13.png)
## 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:
[](http://technhit.in/wp-content/uploads/2016/08/array-exam10.png)
For example considering the following code segment. You are able to access its values from this array as follows:c int num[5]={10,5,17,4,8};
[](http://technhit.in/wp-content/uploads/2016/08/array-exam11.png)c 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. */Array names and pointer variables, playing together:
arrays
array exam2
array exam3
array exam4
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.


