gpt4 book ai didi

C : Why the size of my pizza is not 10 bytes?

转载 作者:行者123 更新时间:2023-12-01 09:22:08 26 4
gpt4 key购买 nike

<分区>

我正在学习 sizeof,这让我很困惑。我决定这样做。

#include<stdio.h>
#include<string.h>
int main(){
char *myWord="PIZZA";
printf ("The size of P is...:%d\n",sizeof("P"));
printf ("The size of I is...:%d\n",sizeof("I"));
printf ("The size of Z is...:%d\n",sizeof("Z"));
printf ("The size of A is...:%d\n",sizeof("A"));
printf ("The size of R is...:%d\n",sizeof("R"));
printf ("The size of PIZZA is...:%d\n",sizeof("PIZZA"));
printf ("The size of *PIZZA is..:%d\n",sizeof(myWord));
return 0;
}

看到结果我很惊讶:

The size of P is...:2
The size of I is...:2
The size of Z is...:2
The size of A is...:2
The size of R is...:2
The size of PIZZA is...:6
The size of *PIZZA is..:4

问题如下:

    为什么数组char和指针char的大小分别是6字节和4字节
    他们怎么会“cruncuh”5 个字母 * 2bytes = 10 个字节。分成6字节和4字节?

让我解释一下我的第二个问题:

据我所知,C 不会按原样 存储字母,而是将字母存储为 ASCII 码。

这段代码证明了这一点

printf("A的ASCII码:%d",myWord[4]);

结果:

A:65的ASCII码

假设我是对的,那么 C 将单词存储为 [80,73,90,65,65],因此 5 个字符 * 2 个字节等于 ..... 10 个字节!但是上面的代码显示了6个字节和4个字节!这就是我在第二个问题中所说的 crunch 的意思。 C 将 10 个字节压缩为 6 个字节和 4 个字节。

感谢任何启发=)

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com