- ..clause 1..: Argument of pointer type
- ..clause 2..: Argument of array type
- ..clause 3..: Strange character of array type argument
- ..clause 4..: The address has been passed.
..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.
The execution result of this program might be as follows.#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 FONT> 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; }
When the function is called, the address of variable value has been passed in this program.
&value = 0F68
pvalue = 0F68
value = 100
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.
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.
The execution result of this program becomes as follows.#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; }
Add the value to array element number 0-9 to the variable in the function.
49
It seems to be able to pass the array in this manner on the face of things as the 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.
The execution result of this program might be as follows.#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 array with only five elements can be passed though the type of the argument is ten elements.
202380394
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.
The execution result of this program becomes as follows.#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; }
In the current argument, even if you change the value of the argument in the called function
array = 15
array = 111
49
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.
There is not a problem even if it rewrites in this manner and it executes it and either it operates.int getaverage(int data[])
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.
Surprisingly, this was the problem and either operated.int getaverage(int *data);
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.
And, both of data are variables of the pointer type in the function.int getaverage(int data[10]); int getaverage(int data[]) int getaverage(int *data);
[ 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.