C 这个简单指针程序输出的解释是什么
我试图理解用于存储变量和指针、指针和指针的地址的大小。结果有点令人困惑 代码如下:C 这个简单指针程序输出的解释是什么,c,pointers,C,Pointers,我试图理解用于存储变量和指针、指针和指针的地址的大小。结果有点令人困惑 代码如下: #include <stdio.h> #include <conio.h> #include <stdlib.h> int main(void) { char *** ppptr_string = NULL; int *** ppptr_int = NULL; double *** ppptr_dbl = NULL;
#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;
}
#包括
#包括
#包括
内部主(空)
{
char***ppptr_string=NULL;
int***ppptr_int=NULL;
双***ppptr_dbl=NULL;
字符c=0;整数i=0;双d=0;
printf(“\n%d%d%d%d%d\n”,大小(&ppptr\u字符串),
sizeof(ppptr_字符串)、sizeof(*ppptr_字符串)、sizeof(**ppptr_字符串),
sizeof(***ppptr_字符串));
printf(“\n%d%d%d%d%d\n”、sizeof(&ppptr\u int)、sizeof(ppptr\u int),
sizeof(*ppptr_int)、sizeof(**ppptr_int)、sizeof(***ppptr_int));
printf(“\n%d%d%d%d%d\n”、sizeof(&ppptr\u dbl)、sizeof(ppptr\u dbl),
sizeof(*ppptr_dbl)、sizeof(**ppptr_dbl)、sizeof(***ppptr_dbl));
printf(“\n sizeof(char)=%d,sizeof(int)=%d,sizeof(double)=%d”,
(三),(一),(四);;
printf(“\n sizeof(&char)=%d,sizeof(&int)=%d,sizeof(&double)=%d”,
规模(c)、规模(i)、规模(d));
getch();
返回0;
}
现在是混乱。我可以看出,在这台机器上,变量地址的长度总是2字节。不管变量的类型和它是否是指针变量。但是为什么这里有这么多的条目,我的尺寸是4?无论类型如何,指针的大小始终为4。存储变量的>地址<的大小为2。指向的内容的大小取决于类型
为什么我在sizeof的输出中得到4s
<我的Borland C++ 5.02的输出
如果您有一个类型
T
和指针上的指针,如T***ptr
,那么ptr
,*ptr
,**ptr
本身就是指针。您可能正在32位系统上工作(或编译32位应用程序),因此sizeof(ptr)==sizeof(*ptr)==sizeof(*ptr)
:
---程序输出---
4 4 4 4 1
4 4 4 4 4
4 4 4 4 8
sizeof(char)=1,sizeof(int)=4,sizeof(double)=8
sizeof(&char)=4,sizeof(&int)=4,sizeof(&double)=4
&ptr
是T***
上的地址/指针,因此其大小也是4。只有将指针解引用到其最大级别(***ptr
)时,才有实际类型,而不是另一个指针。如果有类型T
和指针上的指针,如T***ptr
,则ptr
,*ptr
,**ptr
本身就是指针。您可能正在32位系统上工作(或编译32位应用程序),因此sizeof(ptr)==sizeof(*ptr)==sizeof(*ptr)
:
---程序输出---
4 4 4 4 1
4 4 4 4 4
4 4 4 4 8
sizeof(char)=1,sizeof(int)=4,sizeof(double)=8
sizeof(&char)=4,sizeof(&int)=4,sizeof(&double)=4
&ptr
是T***
上的地址/指针,因此其大小也是4。只有将指针解引用到其最大级别(***ptr
)时,才有实际类型,而不是另一个指针。我认为发生的情况是,局部变量接近(16位)指针,但声明为type*的指针是远(32位)指针
这是在16位英特尔处理器(或“实模式”下的32位处理器)上工作的一种怪癖,例如在DOS中,您只能访问1MB内存(实际上是640KB)。远指针的上16位是段(内存中的64k页),下16位是偏移量
无法再现这一点的回答者最有可能使用32位(或更多)处理器上的32位(或更多)操作系统。我认为发生的情况是,局部变量接近(16位)指针,但声明为type*的指针是远(32位)指针 这是在16位英特尔处理器(或“实模式”下的32位处理器)上工作的一种怪癖,例如在DOS中,您只能访问1MB内存(实际上是640KB)。远指针的上16位是段(内存中的64k页),下16位是偏移量
无法再现这一点的回答者很可能使用32位(或更多)处理器上的32位(或更多)操作系统。conio.h?这是一个MS-DOS编译器吗?请在看到4s和2s时为我不获取提供输出。如果指针大小为4,这是因为这些类型的指针在您的机器上是4字节。语言标准没有规定指针的大小。请添加
sizeof(char*)
、sizeof(double*)
和sizeof(int*)
?另外,您是否正在编译MS-DOS应用程序sizeof(int)=32已经不再那么常见了,你使用什么操作系统?请发布操作系统和版本?conio.h?这是一个MS-DOS编译器吗?请在看到4s和2s时为我不获取提供输出。如果指针大小为4,这是因为这些类型的指针在您的机器上是4字节。语言标准没有规定指针的大小。请添加sizeof(char*)
、sizeof(double*)
和sizeof(int*)
?另外,您是否正在编译MS-DOS应用程序sizeof(int)=32已经不再那么常见了,你使用什么操作系统?请发布操作系统和版本好吗?是的,我使用的是32位机器。这是输出。我在第一列中得到了所有2,并理解了原因。但在其他栏目中获得4s的理由毫无意义。最后一行的机器上没有4s,而是2s,因为它是32位机器。您使用哪种编译器?我无法重现这种行为。此外,第一列中的值2
没有任何意义。32位系统上的地址需要32位=4字节。我使用Borland C++ 5.02编译。
--- Program output ---
4 4 4 4 1
4 4 4 4 4
4 4 4 4 8
sizeof(char) = 1, sizeof(int) = 4, sizeof(double) = 8
sizeof(&char) = 4, sizeof(&int) = 4, sizeof(&double) = 4