字符数组的大小和字符指针的大小 [英] size of character array and size of character pointer

查看:146
本文介绍了字符数组的大小和字符指针的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有件C code的,我不明白是怎么的sizeof(...)函数的工作原理:

I have a piece of C code and I don't understand how the sizeof(...) function works:

#include <stdio.h>

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

在上面的code的sizeof(名字)是6和sizeof(姓氏)为8。

In the above code sizeof(firstname) is 6 and sizeof(lastname) is 8.

鲍比宽5个字符和橡皮头 11宽。我期待 16

But bobby is 5 characters wide and eraserhead is 11 wide. I expect 16.

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

Why is sizeof behaving differently for the character array and pointer to character?

任何一个可以澄清?

推荐答案

字符阵列背着尾随 0 终止子。 是一个指针。在64位系统中指针是8字节宽。

firstname is a char array carrying a trailing 0-terminator. lastname is a pointer. On a 64bit system pointers are 8 byte wide.

这篇关于字符数组的大小和字符指针的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆