ポインタ

ポインタと文字列

#include <stdio.h>

int main(void){

	char *p;
	char a[5]="Hello";
	p = a;

	printf("%c ", a[0]); 	printf("%c\n", *p);
	printf("%c ", a[1]); 	printf("%c\n", *(p+1) );
	
	return 0;
}

上のファイルをコンパイルして,実行すると以下の結果が得られる

H H
e e

メモリ上の様子を表してみる(アドレスはテキトー)
f:id:bunkyu3:20181217200521p:plain:w450

ポインタのポインタ

#include <stdio.h>

int main(void){

	char *q[3];
	char **pp;
	pp = q;
	q[0]="Hello";
	q[1]="World";
	q[2]="!";

	printf("%c ", *(q[0]) ); printf("%c\n", **pp);
	printf("%c ", *(q[1]) ); printf("%c\n", **(pp+1) );
	printf("%c\n", *(q[0]+1) );

	return 0;
}

上のファイルをコンパイルして,実行すると以下の結果が得られる

H H
W W
e

メモリ上の様子を表してみる(アドレスはテキトー)
f:id:bunkyu3:20181217201519p:plain