- ..clause 1..: Substitution of initial value
- ..clause 2..: Display of all elements
- ..clause 3..: Request the number of elements.
- ..clause 4..: Copy of array
The array can be initialized at the same time as declaring as well as the current variable.
Do the initialization of array as follows.
[De] district sequentially switches off and arranges the numerical value to [de] while having inclosed it.型名 配列名[要素数]={0番の数値,1番の数値,2番の数値,・・・};
The execution result of this program becomes as follows.#include <stdio.h> int main(void) { int array[10] = {42,79,13}; printf("array[0] = %d\n",array[0]); printf("array[1] = %d\n",array[1]); printf("array[2] = %d\n",array[2]); printf("array[3] = %d\n",array[3]); printf("array[4] = %d\n",array[4]); return 0; }
It is ..number of elements.. omissible in this method when declaring.
array[0] = 42
array[1] = 79
array[2] = 13
array[3] = 0
array[4] = 0
Because time when the number of elements is specified can be saved, and the count mistake is lost, too#include <stdio.h> int main(void) { int array[] = {42,79,13}; /* The number of elements is omitted */. printf("array[0] = %d\n",array[0]); printf("array[1] = %d\n",array[1]); printf("array[2] = %d\n",array[2]); return 0; }
It is adjusted to one degree to substitute the value for the array only at the declaration.
For instance, the following substitutions cannot be done.
Because it makes an error of thesearray = {42,79,13}; array[10] = {42,79,13};
array[0] = 42; array[1] = 79; array[2] = 13;
The loop of the for sentence can be used to display all elements of the array.
Only being able to do such a usage is the maximum advantage of the array.
The following program is an example of displaying all the numbers of elements in the array.
The execution result of this program becomes as follows.#include <stdio.h> int main(void) { int array[] = {42,79,13,75,19}; int i; for (i = 0;i < 5;i++) { printf("array[%d] = %d\n",i,array[i]); } return 0; }
The point in this program is to be able to use the variable for the array element number.
array[0] = 42
array[1] = 79
array[2] = 13
array[3] = 75
array[4] = 19
If it is this, it is possible to write easily even by the program that displays the data of 10,000 people.
In changing the sentence in the loop the other, substitute the same value as all elements.
The average of the value substituted for all elements can be easily requested.
In the preceding clause, all array elements were displayed by using the loop of the for sentence.
It will repeat automatically requesting the number of elements because it is troublesome.
An immediate method of requesting the number of elements can be calculated though is not prepared.
If it is divided by the size of one element for the size of the entire array, the number of elements is understood.
In C language, there is sizeof ([saizuobu]) operator from which the size of the variable and the array is requested.
Use the sizeof operator as follows.
Applying might be more legible though you may not apply () to the sizeof operator.sizeof(variable and array identifier)
It is assumed array[0] because there must be the 0th elements.sizeof(array) / sizeof(array[0])
The execution result becomes it as well as last time.#include <stdio.h> int main(void) { int array[] = {42,79,13,75,19}; int i; for (i = 0;i < sizeof(array) / sizeof(array[0]);i++) { printf("array[%d] = %d\n",i,array[i]); } return 0; }
Use the for sentence to substitute the value of all elements of a certain array for other arrays.
The execution result of this program becomes as follows.#include <stdio.h> int main(void) { int array1[] = {42,79,13,19,41}; int array2[] = {1,2,3,4,5}; int i; for (i = 0;i < sizeof(array2) / sizeof(array2[0]);i++) { printf("array2[%d] = %d\n",i,array2[i]); } Array2[i] = Array1[i]; } for (i = 0;i < sizeof(array2) / sizeof(array2[0]);i++) { printf("array2[%d] = %d\n",i,array2[i]); } return 0; }
When the result is seen, the value of array1 is copied onto array2.
array2[0] = 1
array2[1] = 2
array2[2] = 3
array2[3] = 4
array2[4] = 5
array2[0] = 42
array2[1] = 79
array2[2] = 13
array2[3] = 19
array2[4] = 41
However, even if the for sentence is not used, the memcpy function can be used.
To use the memcpy function, # include should do the memory.h file.
Because the size of the entire array changes depending on the type and the number of elements of arraysmemcpy(コピー先配列名、コピー元配列名、配列全体のサイズ)
Specify the array identifier by the sizeof operator to copy all elements of the array.
The following program is an example of copying the array by the memcpy function.
The execution result becomes quite the same ahead.#include <stdio.h> #include <memory.h> int main(void) { int array1[] = {42,79,13,19,41}; int array2[] = {1,2,3,4,5}; int i; for (i = 0;i < sizeof(array2) / sizeof(array2[0]);i++) { printf("array2[%d] = %d\n",i,array2[i]); } memcpy(array2,array1,sizeof(array1)); /* All elements of array1 Copy onto array2 */. for (i = 0;i < sizeof(array2) / sizeof(array2[0]);i++) { printf("array2[%d] = %d\n",i,array2[i]); } return 0; }