Table of Contents
This guide covers array in language c with practical context and easy-to-follow details. Use it to understand the subject and apply the information confidently.
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.
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:
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
FAQ
What should I check before following these steps?
Confirm device and software compatibility, save important data, and make sure you have the required permissions, files, and account access.
Why might the process not work?
Common causes include outdated software, missing permissions, incompatible hardware, an unstable connection, or completing a step in the wrong order.
Can I undo the changes if necessary?
That depends on the tool or setting. Use built-in restore options when available, keep a backup, and record the original configuration first.
Reader Comments 0
Sign in with email or Google to join the discussion.