変数とポインター、ポインター-ポインター、およびポインター-ポインター-ポインターを格納するために使用されるアドレスのサイズを理解しようとしました。結果はちょっと混乱しています。
コードは次のとおりです。
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main(void)
{
char *** ppptr_string = NULL;
int *** ppptr_int = NULL;
double *** ppptr_dbl = NULL;
char c=0; int i=0; double d=0;
printf("\n %d %d %d %d %d\n", sizeof(&ppptr_string),
sizeof(ppptr_string), sizeof(*ppptr_string), sizeof(**ppptr_string),
sizeof(***ppptr_string));
printf("\n %d %d %d %d %d\n", sizeof(&ppptr_int), sizeof(ppptr_int),
sizeof(*ppptr_int), sizeof(**ppptr_int), sizeof(***ppptr_int));
printf("\n %d %d %d %d %d\n", sizeof(&ppptr_dbl), sizeof(ppptr_dbl),
sizeof(*ppptr_dbl), sizeof(**ppptr_dbl), sizeof(***ppptr_dbl));
printf("\n sizeof(char) = %d, sizeof(int) = %d, sizeof(double) = %d",
sizeof(c), sizeof(i), sizeof(d));
printf("\n sizeof(&char) = %d, sizeof(&int) = %d, sizeof(&double) = %d",
sizeof(&c), sizeof(&i), sizeof(&d));
getch();
return 0;
}
今混乱。このマシンでは、変数アドレスは常に 2 バイトの長さです。変数の型に関係なく、ポインター変数であるかどうかに関係なく。しかし、ここに非常に多くのエントリがあるのに、なぜサイズが 4 になるのでしょうか? ポインタのサイズは、型に関係なく常に 4 です。変数が格納される >address< のサイズは 2 です。また、ポイントされる内容は、型に応じたサイズになります。
sizeof の出力に 4 が表示されるのはなぜですか??
Borland C++ 5.02 からの私の出力