尝试了解 C 中的字符串大小
我和一个朋友正在为大学学习 C 编程单元。
我们知道,C 语言本身并不存在“字符串”,而是通过字符数组来定义字符串。惊人的!
因此,在处理“字符串”时,正确理解数组和指针显然很重要。
我们很好地理解了指针声明、何时以及何时不取消引用指针,并使用了许多 printf 来测试我们的实验。一切都取得了巨大的成功。
然而,当我们使用这个时:
char *myvar = "";
myvar = "dhjfejfdhdkjfhdjkfhdjkfhdjfhdfhdjhdsjfkdhjdfhddskjdkljdklc";
printf("Size is %d\n", sizeof(myvar));
它会输出 Size is 8
!
为什么是8?显然“myvar”消耗了超过 8 个字节(或者确实如此)?
(我应该明确指出,我非常了解“strlen”。这不是获取字符串长度的练习。这是为了尝试理解为什么 sizeof 为变量 myvar 返回 8 个字节。)
A friend and I are doing a C programming unit for college.
We understand that there is no "string" per se in C, and instead, a string is defined by being an array of characters. Awesome!
So when dealing with "strings" is obvious that a proper understanding arrays and pointers is important.
We were doing really well understanding pointer declaration, when and when not to dereference the pointer, and played around with a number of printf
's to test our experiments. All with great success.
However, when we used this:
char *myvar = "";
myvar = "dhjfejfdhdkjfhdjkfhdjkfhdjfhdfhdjhdsjfkdhjdfhddskjdkljdklc";
printf("Size is %d\n", sizeof(myvar));
and it spits out Size is 8
!
Why 8? Clearly there are more than 8 bytes being consumed by 'myvar' (or is it)?
(I should be clear and point out that I am VERY aware of 'strlen'. This is not an exercise in getting the length of a string. This is about trying to understand why sizeof returns 8 bytes for the variable myvar.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
8是指针的大小。
myvar 是一个指向 char 的指针(因此是 char*),在 64 位系统中指针是 64 位 = 8 字节
要获取以 null 结尾的字符串的大小,请使用以下代码:
8 is the size of the pointer.
myvar is a pointer to char (hence char*) and in 64 bit system pointers are 64 bit = 8 byte
To get size of a null-terminated string use this code :
正如 AbiusX 所说, sizeof 返回 8 的原因是因为您正在查找指针的大小(我猜您在 64 位机器上)。例如,相同的代码片段在我的机器上将返回 4。
C 中的字符串保存为字符数组,后跟空终止符。因此,当您执行此操作时...
它实际上存储在内存中:
如果您读取空终止符,您可能会发现当时内存中恰好存在任何垃圾。因此,为了在 C 中找到字符串的长度,您需要从字符串的开头开始读取,直到 null 终止符。
现在这是一个 O(n) 操作(因为您必须迭代整个数组才能获取大小)。大多数高级语言都有其上层字符串抽象,类似于...
然后,每当对其进行操作时,它们都会跟踪字符串的长度。这大大加快了查找字符串长度的速度,这是一个非常常见的操作。
现在有一种方法可以使用 sizeof 计算出字符串的长度,但我不建议这样做。在数组(不是指针!)上使用 sizeof 将返回数组的大小乘以数据类型大小。 C 可以自动计算出数组的大小,只要它可以在编译时计算出来。
这将打印正确大小的消息。请记住,不建议这样做。请注意,这将打印比字符串中的字符数大一的字符。这是因为它还计算空终止符(因为它必须分配一个足够大的数组来容纳空终止符)。所以它并不是字符串的真正长度(您总是可以减去一)。
Well as AbiusX said, the reason why sizeof is returning 8 is because you are finding the size of a pointer (and I'm guessing you're on a 64-bit machine). For example, that same code-snippet would return 4 on my machine.
Strings in C are kept as an array of characters followed by a null terminator. So when you do this...
It's actually stored in memory as:
If you read past the null terminator, you'll likely just find whatever garbage happens to be in memory there at the time. So in order to find the length of a string in C, you need to start at the beginning of the string and read until the null terminator.
Now this is an O(n) operation (because you have to iterate over the entire array to get the size). Most higher level languages have their upper level abstraction of strings as something similar to...
And then they just keep track of how long the string is whenever they do an operation on it. This greatly speeds up finding the length of a string, which is a very common operation.
Now there is one way you can figure out the length of a string using sizeof, but I don't suggest it. Using sizeof on an array (not a pointer!) will return the size of the array multiplied by the data type size. And C can auto-figure out the size of an array as long as it can be figured out at compile-time.
That will print the correct size of the message. Remember, this is NOT suggested. Notice that this will print one greater than the number of characters in the string. That's because it also counts the null terminator (as it has to allocate an array large enough to have the null terminator). So it's not really the real length of the string (you can always just subtract one).
myvar
是一个指针。您似乎使用的是 64 位机器,因此sizeof
返回 8 字节大小。您可能正在寻找的是 strlen()。myvar
is a pointer. You seem to be on a 64-bit machine, sosizeof
returns 8 byte in size. What you're probably looking for instead is strlen().正如AbiusX所说,8是指针的大小。
strlen
可以告诉你字符串的长度(手册页 )。Like AbiusX said, 8 is the size of the pointer.
strlen
can tell you the length of the string (man page).