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


  Delivery of information by argument   

  1. ..clause 1..: Argument of pointer type
  2. ..clause 2..: Argument of array type
  3. ..clause 3..: Strange character of array type argument
  4. ..clause 4..: The address has been passed.

[1] Argument of pointer type

..self-made function.. How to use Chapter 11, though it explained how to make
Here, it explained the method of using the return value as a method of returning information from the function.

Though the easiest method the return of information by using the return value makes a mistake and doesn't exist
In this method, only one information can be returned at any time.
Wanting to return two information or more are inconvenient.

For that case, information can be returned by using the argument of the pointer type.
It is not specially special even if it is said the argument of the pointer type.
Only, usual argument and no any kooky [wari] the type of the argument is a pointer type alone.

In C language, pass the copy of the value of former variable when you pass the function information.
Be not to call such a method call-by-value, and the feature to changed the value of former variable.
There is no difference in the principle to which the copy of the value is passed even if it is an argument of the pointer type.

The pointer type is used still because the pointer type can receive the address.
If you specify the address of the variable that already exists when the function is called
If you substitute the received address for the pointer variable in the called function
Returned information can be substituted by usually switching the pointer variable to the variable mode now.
Returned information will be memorized in the variable specified on the call side.

The following program is an example of returning information by actually using the argument of the pointer type.

 
#include <stdio.h> 

void func(int *pvalue); /* Prototype declaration */

int main(void) 
{
	int value = 10; 
	printf("&value = %p\n",&value); 
	func(&value); /* */ that passes address Printf("value =%d\n",value);
	return 0; 
}

void func(int *pvalue) 
{
	printf("pvalue = %p\n",pvalue); 
	*pvalue = 100; /* It switches to the variable mode and 
	substitute it usually */  Return;
}
The execution result of this program might be as follows.
This Because it is a result in LSIC-86, the addresses are two bytes.

&value = 0F68
pvalue = 0F68
value = 100
When the function is called, the address of variable value has been passed in this program.
It is an address value (It is 0F68 this time) to the last that is passed to the func function.
Because the address value is substituted for the pointer variable in the func function
Naturally, the address passed to the func function and the received address are the same.

When the address value is substituted for the pointer variable
Because the memory can be freely read and written by switching to the variable mode usually
Consequently, it calls from the called function and contents of former variable are rewritten.

Functions have been called by adhering up to now are similar all mechanisms.
This usage is a usage of the most popular pointer in C language.

It returns to contents.


[2] Argument of array type

The array can be made an argument though it has not handled up to now.
However, a character different from a usual argument becomes difficult a lot at the array.
Make the function with the argument of the array type in the method the same as the current for the time being.

The argument assumes the array of element 10 by the int type, and makes the function from which the average of the value substituted for the array is requested.
It becomes as follows when mounting by the method the same as the current.

 
#include <stdio.h> 

int getaverage(int data[10]);  

int main(void)  
{
	int average,array[10] = {15,78,98,15,98,85,17,35,42,15};  
	average = getaverage(array);  
	printf("%d\n",average);  
	return 0;  
}

int getaverage(int data[10])  
{
	int i,average = 0;  
	for (i = 0;i < 10;i++) { 
		average += data[i]; 
	}
	return average / 10;
}
The execution result of this program becomes as follows.

49
Add the value to array element number 0-9 to the variable in the function.
The result is divided by ten at the end and the mean value is requested.

It seems to be able to pass the array in this manner on the face of things as the argument.

It returns to contents.


[3] Strange character of array type argument

Though it explained the method of using the array as an argument in the preceding clause
This function could not be the current argument, and is strange.

First of all, the number of array elements is disregarded.
The following program is an example of on purpose passing the array of number 5 of elements.

 
#include <stdio.h> 

int getaverage(int data[10]);  

int main(void)  
{
	int average,array[5] = {15,98,98,17,42};    /* The number of elements is 5 */ average =  Getaverage(array);  
	printf("%d\n",average);  
	return 0;  
}

int getaverage(int data[10])  
{
	int i,average = 0;  
	for (i = 0;i < 10;i++) { 
	average += data[i]; 
	}
	return average / 10;
}
The execution result of this program might be as follows.

202380394
The array with only five elements can be passed though the type of the argument is ten elements.
As a result, ten elements are forcibly processed on the function side, and a strange result.

In addition, it changes into the call side when the value of the array is changed as a strange phenomenon in the function.
The following program is an example of changing the value of the array in the function.

 
#include <stdio.h> 

int getaverage(int data[10]);  

int main(void)  
{
	int average,array[10] = {15,78,98,15,98,85,17,35,42,15};  
	printf("array[3] = %d\n",array[3]);  
	average = getaverage(array);  
	printf("array[3] = %d\n",array[3]);  
	printf("%d\n",average);  
	return 0;  
}

int getaverage(int data[10])  
{
	int i,average = 0;  
	for (i = 0;i < 10;i++) { 
		average += data[i]; 
	}
	data[3] = 111;    /* The value of the argument vector is changed */  return average/ 10;
}
The execution result of this program becomes as follows.

array = 15
array = 111
49
In the current argument, even if you change the value of the argument in the called function
Though it calls and the value of former argument never never changed
In the array, the change calling ahead influences the call origin why.
This cannot be call-by-value.

It returns to contents.


[4] The address has been passed.

In the preceding clause, it explained a strange character of the argument of the array type.
Such a phenomenon is that the array is call-by-value [sareteireba] absolutely impossible.
In a word, it is not call-by-value [sare] if it says oppositely as for the array.

However, it succeeds in passing the array to the function actually and the calculation of the mean value.
In a word, it is not wrong true that the array has been passed in some shape.

Let's experiment a little to verify it on this point.
I see in the argument of the array type ..disregard.. in the preceding clause first of all. the number of elements
How do it become it if the number of elements is not rather specified if it is it?
In a word, change the function as follows.

 int getaverage(int data[])
There is not a problem even if it rewrites in this manner and it executes it and either it operates.
Moreover, without specifying the number of elements by the prototype declaration
What error [mode] when number of elements is applied in declaration of actual function.
It is understood that the number of elements is completely disregarded because of this.

However, how has the value of the array been passed disregarding the number of elements?
When the array is passed, only the number of numbers of elements will copy the value if it thinks usually.
However, because the number of elements is disregarded, such a method cannot be used.

Here, I want to do another experiment.
Though it changes until the call origin when the value of the array is changed in the preceding clause by the called function
This phenomenon looks like time that used the argument of the pointer type well.
In a word, the stern is thought ..might pass not the array but the address...
The function was changed by way of experiment as follows.

 int getaverage(int *data);
Surprisingly, this was the problem and either operated.
In this, all causes of a strange phenomenon ahead turned out.
In a word, the array had not been passed but the first address of the array had been passed.

Only if the first address of the array is passed, the number of elements is not related at all.
Moreover, because the array by the called function will indicate the same memory area as the call origin
When the value of the array is changed by the called function, it is natural that the call origin is changed.

First of all, when this is brought together, the following three are dummy argument declarations of the same meaning.
However, it is only a dummy argument declaration of the function that this three it becoming the same meaning.

 int getaverage(int data[10]);  int getaverage(int data[]) int getaverage(int *data);
And, both of data are variables of the pointer type in the function.
And, use the array of quite the same memory area at calling ahead and the call origin.

[ To   which do make it   ].
Though it doesn't know which I may use when this three are the same meanings though there might be a waverer, too
Shape to omit the number of the second elements is recommended to be used as an author.
Because the third declaration is confusing with a usual pointer type.
It is understood to receive the array if it is the second declaration specifying it.
The people from whom the first declaration becomes accustomed to C language are seen like a childish declaration.

It returns to contents.


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