7
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

関数の引数にポインタを渡した時の簡単な違い

Last updated at Posted at 2016-04-10

Goの勉強をしていたら再発見(というより復習)しまして、基本が抜け落ちているなぁと痛感…。
C言語を勉強した時に学んだ気がするんですが、完全に頭から抜けていたので書いておきます。
この記事では例としてC,Goで例示します。

値渡し

まずは普通に変数の値を関数に渡してみます。

# include<stdio.h>

int plus1(int x){
	x = x + 1 ;
	return x ;
}

int main(void){
	int a ;
	a = 1 ;
	
	plus1(a);
	
	printf("a = %d\n",a);//a = 1と表示される
	return 0;
}
package main
import "fmt"

func plus1(x int) int{
	x = x + 1
	return x
}

func main(){
	a := 1
	a1 := plus1(a)
	
	fmt.Println("a =", a)	//a = 1と表示される
	fmt.Println("a + 1 =", a1)	//a + 1 = 2と表示される
}

ポインタ渡し

次にポインタを渡してみます。

# include<stdio.h>

int plus1(int *x){
	*x = *x + 1 ;
	return *x ;
}

int main(void){
	int a ;
	a = 1 ;
	
	plus1(&a);
	
	printf("a = %d\n",a);//a = 2と表示される
	return 0;
}
package main
import "fmt"

func plus1(x *int) int{
	*x = *x + 1
	return *x
}

func main(){
	a := 1
	a1 := plus1(&a)
	
	fmt.Println("a =", a)	//a = 2と表示される
	fmt.Println("a + 1 =", a1)	//a + 1 = 2と表示される
}

結果が変わりました。

まとめ

変数aを普通に渡すと、関数にはaをコピーしたものが渡されます。なので、元々のaには変更が適応されません。
ポインタを渡した場合は、aが格納されているアドレスを直接修正しているので、「aに1を足す」という処理から「aが指しているアドレスの中の数値に1を足す」という処理に変わっています。
なので最終的に同じ場所を見ている変数aの値も1から2になります。

※ポインタを渡した場合もメモリアドレスのコピーを渡しているだけのようです。コメントにてshiracamusさんよりご指摘いただきましたので、詳しくはコメント欄を参照してください。(とても解りやすく解説して頂いてます。)

最後に

できる人からしたら当たり前のことですが、今まで曖昧にしたままだったのでこの機会に復習しました。少しでも誰かのお役に立てばと思います。

7
10
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?