gpt4 book ai didi

c - 字符数组的大小和字符指针的大小

转载 作者:太空狗 更新时间:2023-10-29 16:30:22 28 4
gpt4 key购买 nike

我有一段 C 代码,但我不明白 sizeof(...) 函数是如何工作的:

#include <stdio.h>

int main(){
const char firstname[] = "bobby";
const char* lastname = "eraserhead";
printf("%lu\n", sizeof(firstname) + sizeof(lastname));
return 0;
}

在上面的代码中,sizeof(firstname) 是 6,sizeof(lastname) 是 8。

但是 bobby 是 5 个字符宽,eraserhead 是 11 个字符宽。我希望 16

为什么 sizeof 对字符数组和字符指针的行为不同?

谁能解释一下?

最佳答案

firstname 是一个带有尾随 0 终止符的 char 数组。 lastname 是一个指针。在 64 位系统上,指针为 8 字节宽。

关于c - 字符数组的大小和字符指针的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17260242/

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