アドレスの埋まる順番

今読んでいる教科書中の実行結果と、自分の環境下での実行結果が違ったのでメモ。

#include<stdio.h>
//tes5.c

int main()
{
	int i;
	int num;
	int ary[5];
	
	printf("The address of a is %p\n", &num);
	printf("The address of ary is %p\n", &ary);
	
	for(i=0;i<5;i++){
		printf("The address of ary[%d] is %p\n", i, &ary[i]);
	}
	
	return 0;
}

% tes5.exe
The address of a is 0012FF88
The address of ary is 0012FF74
The address of ary[0] is 0012FF74
The address of ary[1] is 0012FF78
The address of ary[2] is 0012FF7C
The address of ary[3] is 0012FF80
The address of ary[4] is 0012FF84

  • 先に宣言された変数ほど、大きいアドレスが割り当てられる。
  • 配列は、最後の要素に一番大きいアドレスが割り当てられる。