- ..clause 1..: Usage like array
- ..clause 2..: Writing only for pointer
- ..clause 3..: Old, bad pointer operation
- ..clause 4..: Let's forget the address.
In the foregoing paragraph, it explained that the address of the leading element of the array had been passed to pass the function the array.
This..inside..obviously..unnatural..comment..show..line.#include <stdio.h> int getaverage(int *data); 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) { int i,average = 0; It is ?*/ though it is average += data[i] ;/* pointer variable. } return average / 10; }
This easily explains paragraph 3.
Though the role of [] is an operator that specifies the array element number
The mechanism is merely added up to the address of array identifier.
In a word, it will be said that anything is good if it is an address value even if it is not separately an array.
Regardless of the presence of the sign of [] when you describe the array identifier in the expression when explaining in detail
The array identifier is treated as an address to the leading element of the array (pointer value).
And, add up only the value of the number to the address when you apply [] to the array identifier.
As a result, it is treated as an element of the number to have been added up.
It is understood to be able to use the pointer variable from this like the array if it is a memory that can be used.
[ Difference between declaration and expression ]
Specify the number of elements by [] when you declare the array.
When you use the array element though the number is specified by []
Actually, this even two are another completely signs.
Though [] when it declares has the meaning of specifying the number of elements
[] used in the expression is an operator added up to the address.
In C language, I tend to want to use the same sign for the usage that looks like.
Therefore, there seem to be a lot of parts where the same sign as a different meaning is allocated.
The execution result of this program becomes as follows.#include <stdio.h> int main(void) { int *data; int i,average = 0,array[10] = {15,78,98,15,98,85,17,35,42,15}; data = array; /* Substitute the address of the array for the pointer variable */. It is possible to use like average += data[i] ;/* array */. } printf("%d\n",average / 10); return 0; }
Usually though there is no necessity for doing such a confusing thing at all
49
[ The array and the pointer are complete different things ].
A lot of people seem to misunderstand the array and the pointer.
The array is a method of treating a lot of variables collectively by order credit.
The pointer is a method of making the short cut of the variable.
It is related to the design of the array to be able to do the usage that looks like yet.
In C language, use addition to the address as a means to achieve the array.
However, because it has the function as which the pointer variable is the same by chance
When the pointer variable is used, it can be equal with the array.
Therefore, though the pointer and the array are confused easily
The array is a fixed variable that shows the head of a lot of variables to the last.
The pointer variable substitutes the address of a favorite variable.
It is a changeable variable that can use a favorite memory area.
In the preceding clause, it explained that it was possible to use it similarly by substituting the address of the array for the pointer variable.
To be able to use it similarly is to mean the element number can be specified by the [] operator.
However, there is to tell the truth writing for the pointer variable in the pointer variable.
Write this as follows by the writing that is called a pointer operation.
* at the head is an operator to usually switch the pointer variable to the variable mode.*(ポインタ変数 + 要素番号)
The following program is an example of rewriting the program ahead by this writing.
[ Pointer operation ]
Writing that adds and subtracts to pointer variable and uses array element.
It was widely used for foolish reasons why it seemed to be C language in old times.
It accesses the array by using the pointer operation without using [].#include <stdio.h> int main(void) { int *data; int i,average = 0,array[10] = {15,78,98,15,98,85,17,35,42,15}; data = array; /* Substitute the address of the array for the pointer variable */. Average +=*(data + i) ;/* pointer operation */ } printf("%d\n",average / 10); return 0; }
In addition, the pointer variable also has the following writing using being able to change the value.
Generally, here is often pointed at if it is said the pointer operation.
The explanation is necessary for this program because there are a lot of considerably confusing parts.#include <stdio.h> int main(void) { int *data; int average = 0,array[10] = {15,78,98,15,98,85,17,35,42,15}; Here /* is paid attention */ Average +=*data; } printf("%d\n",average / 10); return 0; }
In a word, increase the value of the pointer variable and in accessing it
It is a method of the sequential access to each one of the array element.
It is writing used from the reason that this is high-speed well in C language.
Because whenever it accesses the array when the array is usually used by [], it is necessary to add up.
It adds up however because of ending only in doing at the loop once if it is a pointer operation.
In the preceding clause, it explained the pointer operation of the writing accessed the array element by the pointer variable.
However, ask you. Do you think that that writing is comprehensible?
At least, the author doesn't think comprehensible.
First of all, it is obvious [desu] which understanding and the chair though the following two are quite the same meanings.
Moreover, the pointer operation in an increase by using ++ etc. are more awful.data[5] *(data + 5)
In addition, though the pointer operation in an increase by using ++ was written it is high-speedfor (i = 0;i < 10;i++) { & nbsp;average += data[i]; } for (data = array;data != &array[10];data++) { & nbsp; average += *data; }
When C language is just made though there was no such compiler
A lot of compilers devise the extent in modern days.
The ++ pointer operation is considerably used from the remaining influences in old times though there are still a lot of users
[ Pair seeing ]
Though the compiler for most personal computers does appropriate optimization
Seem not to limit building in (computer built into the consumer electronic etc.) as so.
Moreover, it is easy only to be simple the mechanism of CPU, and to connect directly a little difference with the performance.
Note it when you do the programming for an electronic equipment.
Believe that the ++ pointer operation is the way it should be of C language at the present age.
++ There seem to be a lot of people who abuse the pointer operation.
Though it is thought that it is more comprehensible to use [] if it is a normal person kind
Oh dear, there is no method because they are each favor.
The function of the pointer variable was explained almost and carried out here.
However, when actually programming
Forget it is a variable to which the pointer variable memorizes the address neatly.
Because..pointer variable..true..How to use..variable..short cut..use.
The address is not operated never.
In other words, it is internal processing of the compiler that the pointer variable memorizes the address.
It is because of the irrelevance what kind of mechanism internal process as long as the usage is understood.
The computer can be used even if it doesn't know the mechanism of the computer at all and the same thing.
The purpose of a lot of people's stumbling by the pointer variable is to consider only memorizing the address.
Even if it doesn't know such an internal mechanism, the pointer variable can be easily used.
Set the short cut putting up variable [niwo], put * sign, usually make to the variable mode, and use it.
It is unrelated of what like the address etc. as long as it uses it according to this procedure.
However, it is [deruko] and ..sometimes very mysterious bug.. [gaarimasu] in C language.
In this case, the purpose of many of the causing is to have made a mistake in the usage of the pointer variable.
Recall that it is a variable to which the pointer variable memorizes the address when the bug is corrected.
Whether an address wrong somewhere is substituted will have to be examined.