The post Using Arrays C Language with Example appeared first on Tech Blog.
]]>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.
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]
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};
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 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 post Using Arrays C Language with Example appeared first on Tech Blog.
]]>