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


  Strange relation between array and pointer   

  1. ..clause 1..: Usage like array
  2. ..clause 2..: Writing only for pointer
  3. ..clause 3..: Old, bad pointer operation
  4. ..clause 4..: Let's forget the address.

[1] Usage like array

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.
By the way, though the following program is a program of the foregoing paragraph made the argument of the pointer type
Is this program seen, and is not an unnatural part somewhere found?
 
#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..inside..obviously..unnatural..comment..show..line.
Because this variable data is a pointer variable, and not an array.
What is being able to specify the element number by not related of [nimo], and using []?

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.


[ 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.

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.
The following program is an example that
uses the pointer variable like the array.
 
#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;
}
The execution result of this program becomes as follows.

49
Usually though there is no necessity for doing such a confusing thing at all
When it came to secure a dynamic memory, it needs it sooner or later.

[ 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.

It returns to contents.


[2] Writing only for pointer

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.
Add up to the address value of the pointer variable for the element number applying parentheses.
In the usual switch of the address value added up to the variable mode,
It is a method of the access to a previous memory advanced from the initial address only by a specified number.

[   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.
The following program is an example of rewriting the program ahead by this writing.
 
#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;
}
It accesses the array by using the pointer operation without using [].
Of course, the execution result becomes quite the same ahead.

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.

 
#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;
}
The explanation is necessary for this program because there are a lot of considerably confusing parts.
First of all, when the for sentence is begun, the address of the array is substituted for pointer variable data.
And, though data++ is specified for an update
This meaning is an operation that expands one address in data during the minute thoroughly to the time of the current variable.
(Increase it only by the size of the type that the pointer variable indicates accurately. )
And, repeat until the pointer variable (Count from 0) reaches the same value as the tenth element.

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.

It returns to contents.


[3] Old, bad 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.


data[5]
*(data + 5)
Moreover, the pointer operation in an increase by using ++ etc. are more awful.
The human race who thinks that the following two are seen, and the below is more comprehensible doesn't think that it exists.
 
for (i = 0;i < 10;i++) {    &
nbsp;average += data[i]; 
}

for (data = array;data != &array[10];data++) { &
nbsp;   average += *data; 
}
In addition, though the pointer operation in an increase by using ++ was written it is high-speed
Actually, this is an old story.
The performance of the compiler has improved rapidly now.
And, if a modern compiler finds the loop accessed by []
Automatically, replace with writing like the pointer operation that increases by ++ and compile.

When C language is just made though there was no such compiler
A lot of compilers devise the extent in modern days.


[   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.
The ++ pointer operation is considerably used from the remaining influences in old times though there are still a lot of users
We will recommend accessing the array by using comprehensible [] as an author.

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.

It returns to contents.


[4] Let's forget the address.

The function of the pointer variable was explained almost and carried out here.
Value the variable the pointer variable that memorizes the address.
It has explained various phenomena that coil round it.

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.

It returns to contents.


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