Array in Language C

The C programming language provides a data structure called an array, stored in a set of data of the same type with fixed length. An array is used to store data sets, but it is useful if you think of an array of variables with the same type.

The C programming language provides a data structure called an array , stored in a set of data of the same type with fixed length. An array is used to store data sets, but it is useful if you think of an array of variables with the same type.

Instead of declaring variables in a discrete way, like variables number0, number1, . and number99, you can declare an array of values ​​like numbers [0], numbers [1] and . numbers [99] to represent the separate values. A specific component of the array can be accessed via index (index).

All arrays include adjacent memory locations. The lowest address corresponds to the first component and the highest address corresponding to the last component of the array.

Array in Language C Picture 1

Declare the array in C

To declare an array in C language, the program determines the type of variable and the number of elements required by that variable as follows:

 Kieu Ten_mang [ Kich_co_mang ]; 

This is a one-dimensional array. Kich_co_mang must be an integer greater than 0 and Kieu must be valid in C language. For example, declare an array of 10 elements called sohangban with type int, use the following statement:

 int sohangban [ 10 ]; 

Sohangban is an array variable that can accommodate 10 int elements.

Initialize array in C

You can initialize arrays in C or each element or use a statement like this:

 int sohangban [ 5 ] = { 34 , 56 , 23 , 124 , 67 }; 

The number of values ​​in quotation marks {} cannot be greater than the number of declared elements in square brackets [].

If you omit the array size, that array is large enough to hold the initialized values:

 int sohangban [] = { 34 , 56 , 23 , 124 , 67 }; 

You will create exactly one string with the same value as the above string by assigning each element one by one. Here is an example when assigning values ​​to an element of an array:

 sohangban [ 4 ] = 67 ; 

The above statement assigns the 5th value of array value 67. All arrays have the first index (index) equal to 0, this is called the basic index and the last element of the array has the index equal to the size of the array minus 1. Here is a graphical representation of the above declared string through the index:

Array in Language C Picture 2

Access array elements in C

An array is accessed by indexing in the name of the array. Here is a way to access an array value:

 int luonghangban = sohangban [ 9 ]; 

The above statement takes the 10th element of the array and assigns this value to luonghangban variable. Here is an example of use with all the above descriptions:

 #include int main () { int n [ 10 ]; /* mang n gom 10 so nguyen */ int i , j ; /* khoi tao cac phan tu trong mang ve gia tri 0 */ for ( i = 0 ; i < 10 ; i ++ ) { n [ i ] = i + 100 ; /* Thiet lap phan tu tai vi tri i thanh i + 100 */ } /* hien thi gia tri cac phan tu trong mang */ for ( j = 0 ; j < 10 ; j ++ ) { printf ( "Phan tu [%d] = %dn" , j , n [ j ] ); } printf ( "n===========================n" ); printf ( "QTM chuc cac ban hoc tot! n" ); return 0 ; } 

Compile and run the above C program to see the results:

According to Tutorialspoint

Previous lesson: Scope rules in programming C

Next lesson: Cursor in C

4 ★ | 1 Vote | 👨 109 Views
« PREV POST
NEXT POST »