※スタイルシート未対応ブラウザではレイアウトを正確に再現できません。
  > | advanced by | contents  | that returns in <    
                   < [modosusu] > Color magazine monochrome light and shade   font predetermined, Gothic Ming-style type longhand   size Konaka large   standard  


  How to use of the array.   

  1. ..clause 1..: Substitution of initial value
  2. ..clause 2..: Display of all elements
  3. ..clause 3..: Request the number of elements.
  4. ..clause 4..: Copy of array

[1] Substitution of initial value

The array can be initialized at the same time as declaring as well as the current variable.
Do the initialization of array as follows.


型名 配列名[要素数]={0番の数値,1番の数値,2番の数値,・・・};
[De] district sequentially switches off and arranges the numerical value to [de] while having inclosed it.
Because everything need not be specified, the number of numerical values is made below the number of array elements.
When it is fewer than the number of array elements, all 0 is substituted for the remainder.
The following program is an example of initializing and displaying the array.
 
#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;
}
The execution result of this program becomes as follows.

array[0] = 42
array[1] = 79
array[2] = 13
array[3] = 0
array[4] = 0
It is ..number of elements.. omissible in this method when declaring.
In that case, the number of elements only of numbers of specified numerical values is secured.
The following program is an example of omitting the number of elements and declaring the array.
 
#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;
}
Because time when the number of elements is specified can be saved, and the count mistake is lost, too
You will omit it when you can substitute an initial value.

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.


array = {42,79,13};
array[10] = {42,79,13};
Because it makes an error of these
Substitute it one by one even if it is troublesome when you want to substitute the value after the declaration.

array[0] = 42;
array[1] = 79;
array[2] = 13;

It returns to contents.


[2] Display of all elements

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.

 
#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 execution result of this program becomes as follows.

array[0] = 42
array[1] = 79
array[2] = 13
array[3] = 75
array[4] = 19
The point in this program is to be able to use the variable for the array element number.
If the value of the variable is changed, it can directly access the element of various numbers.
In a usual variable that is not the array, five printf functions are necessary for the display of five elements.
Only change the numerical value of the for sentence if it is an array though it is 100 pieces necessary to display 100 pieces.

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.

It returns to contents.


[3] Request the number of elements.

In the preceding clause, all array elements were displayed by using the loop of the for sentence.
With this, it is necessary to count the number of elements though five of the frequencies was repeatedly specified directly.
Whenever the number of elements increases, it is necessary to rewrite the for sentence in this method.

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.

 
sizeof(variable and array identifier)
Applying might be more legible though you may not apply () to the sizeof operator.
Do as follows to request the number of elements of arrays array by using this operator.
 sizeof(array) / sizeof(array[0])
It is assumed array[0] because there must be the 0th elements.
The following program is an example of having rewritten the program of the preceding clause by this method.
 
#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;
}
The execution result becomes it as well as last time.
Display only the number automatically when you change the number of array elements.

It returns to contents.


[4] Copy of array

Use the for sentence to substitute the value of all elements of a certain array for other arrays.

 
#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;
}
The execution result of this program becomes as follows.

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
When the result is seen, the value of array1 is copied onto array2.

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.


memcpy(コピー先配列名、コピー元配列名、配列全体のサイズ)
Because the size of the entire array changes depending on the type and the number of elements of arrays
In this function, make the size obtained by the sizeof operator a radical.

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.

 
#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;
}
The execution result becomes quite the same ahead.
Because only a specified size is copied even if the array of the copy origin is larger than the copy destination
The problem might occur if it copies it from a lot of arrays of the number of elements that are.

It returns to contents.


< - It is advanced -> | in | head that returns  to returning  next |.