勉強になったサイト



ポインタの概念は、



*の混乱


int* i; int* intのポインタと読む。 intのポインタi
*i は,iの指す値と読む。ワイルドカードだと思って、読み方で区別する。



配列とポインタの混乱



 p[i]     *(p+i)
上のふたつの式は、全く同等である。ポインタPからi番目の値を指してるに過ぎない。
ポインタpに加算してるiはpの指すオブジェクトのサイズ分のインクリメントである。


 void func(int *a){ }  void func(int a[]){}
上の二つは同等である。両方共ポインタの宣言である。配列の宣言が、ポインタになるのは、関数の仮引数の場合のみである。

void func(char *s){ }     void func(char s[]){ }  charの場合も同じ。



配列名+i を表示させてみた

#include <stdio.h>

int main()
{
   int numa[5] = {24,35,38,60,101};
   int i = 0;
   
   
		for(i=0;i<5;i++){
		
			printf("numa[%d] = %d\t",i,numa[i]);            //配列の中身を表示
			printf("address of numa[%d] = %d\n",i,numa+i); //配列の最初からアドレスを表示 
			
			}
	
return 0;	
}
結果
numa[0] = 24    address of numa[0] = 2686712
numa[1] = 35    address of numa[1] = 2686716
numa[2] = 38    address of numa[2] = 2686720
numa[3] = 60    address of numa[3] = 2686724
numa[4] = 101   address of numa[4] = 2686728

並びで4バイトづつ増えてます。

double numa[5]にすると下のようになりました。

numa[0] = 24.000000     address of numa[0] = 2686688
numa[1] = 35.000000     address of numa[1] = 2686696
numa[2] = 38.000000     address of numa[2] = 2686704
numa[3] = 60.000000     address of numa[3] = 2686712
numa[4] = 101.000000    address of numa[4] = 2686720

8バイトづつ増えてます。

char型にすると

#include <stdio.h>

int main()
{
   char numa[] = "confortable";
   int i = 0;
   
   
for(i=0;i<5;i++){
printf("numa[%d] = %c\t",i,numa[i]); 
printf("address of numa[%d] = %d\n",i,numa+i);
}
return 0;
}


numa[0] = c   address of numa[0] = 2686720
numa[1] = o     address of numa[1] = 2686721
numa[2] = n     address of numa[2] = 2686722
numa[3] = f     address of numa[3] = 2686723
numa[4] = o     address of numa[4] = 2686724

1バイトづつです。