sizeof的结果

HostOOP 2003-03-06 02:51:42
char *cp;

union {
short s;
char c[2];
} un;

问:
sizeof(cp) = ?
sizeof(un) = ?

为什么????
...全文
91 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
langzi8818 2003-03-06
  • 打赏
  • 举报
回复
4和2,
我想可能变量太少,所以没有考虑位对齐
hiliyun 2003-03-06
  • 打赏
  • 举报
回复
要看是什么机器了,我在aix下sizeof(cp)==8,也就是aix下int为64位.
但是如果要用turboc2。0的话sizeof(cp)==2所以这主要是看你的机器是多少位得了:)
chinajiji 2003-03-06
  • 打赏
  • 举报
回复
sizeof(un)如果考虑位对齐调整,可能等于4
chinajiji 2003-03-06
  • 打赏
  • 举报
回复
sizeof(cp) == sizeof(char*) = sizeof(void*) == sizeof(int) == 4(vc6.0中)
sizeof(un) == (sizeof(short) > sizeof(c) ? sizeof(short), sizeof(c)) == 2 (vc6.0);
yang0008 2003-03-06
  • 打赏
  • 举报
回复
up
liuheliaoshi7879 2003-03-06
  • 打赏
  • 举报
回复
第一问,答案是一个字符的长度,第二问是取short , char两种类型所占长度的最大值,因为第二个变量是一个联合体类型,计算它的长度办法就是取其成员变量中长度最大值。
yuanhen 2003-03-06
  • 打赏
  • 举报
回复
sizeof(cp) = 4(字节);我们的CPU是32位的(即4个字节)
sizeof(un) = sizeof(short int) 16位(2字节)
32-bit data types, sizes, and ranges

Type Size (bits) Range Sample applications
unsigned char 8 0 <= X <= 255 Small numbers and full PC character set
char 8 -128 <= X <= 127 Very small numbers and ASCII characters
short int 16 -32,768 <= X <= 32,767 Counting, small numbers, loop control
unsigned int 32 0 <= X <= 4,294,967,295 Large numbers and loops
int 32 -2,147,483,648 <= X <= 2,147,483,647 Counting, small numbers, loop control
unsigned long 32 0 <= X <= 4,294,967,295 Astronomical distances
enum 32 -2,147,483,648 <= X <= 2,147,483,647 Ordered sets of values
long 32 -2,147,483,648 <= X <= 2,147,483,647 Large numbers, populations
lbaby 2003-03-06
  • 打赏
  • 举报
回复
sizeof cp=一个指针的长度

sizeof un= max{short, sizeof char *2}//取二者较大者

70,037

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧