C ++什么是字符串文字的数据类型? [英] C++ what is the datatype of string literal?

查看:127
本文介绍了C ++什么是字符串文字的数据类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对字符串文字的数据类型感到困惑。是 const char * 还是 const char ?深入了解。感谢

I'm confused about the datatype of a string literal . Is it a const char * or a const char ?. Insights are appreciated. Thanks

推荐答案

这是一个 const char [N] N 的长度。 字符串加上一个用于终止 NUL (或者只是字符串的长度,如果你定义字符串的长度已经包括 NUL

It is a const char[N] (which is the same thing as char const[N]), where N is the length of the string plus one for the terminating NUL (or just the length of the string if you define "length of a string" as already including the NUL).

这是为什么你可以做 sizeof(hello) - 1 以获取字符串中的字符数(包括任何嵌入的 NUL s);如果它是一个指针,它不会工作,因为它将永远是您的系统上的指针大小(减去一个)。

This is why you can do sizeof("hello") - 1 to get the number of characters in the string (including any embedded NULs); if it was a pointer, it wouldn't work because it would always be the size of pointer on your system (minus one).

这篇关于C ++什么是字符串文字的数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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