C->字符串的大小始终为 8
#include "usefunc.h" //don't worry about this -> lib I wrote
int main()
{
int i;
string given[4000], longest = "a"; //declared new typdef. equivalent to 2D char array
given[0] = "a";
printf("Please enter words separated by RETs...\n");
for (i = 1; i < 4000 && !StringEqual(given[i-1], "end"); i++)
{
given[i] = GetLine();
/*
if (sizeof(given[i]) > sizeof(longest))
{
longest = given[i];
}
*/
printf("%lu\n", sizeof(given[i])); //this ALWAYS RETURNS EIGHT!!!
}
printf("%s", longest);
}
为什么总是返回8???
#include "usefunc.h" //don't worry about this -> lib I wrote
int main()
{
int i;
string given[4000], longest = "a"; //declared new typdef. equivalent to 2D char array
given[0] = "a";
printf("Please enter words separated by RETs...\n");
for (i = 1; i < 4000 && !StringEqual(given[i-1], "end"); i++)
{
given[i] = GetLine();
/*
if (sizeof(given[i]) > sizeof(longest))
{
longest = given[i];
}
*/
printf("%lu\n", sizeof(given[i])); //this ALWAYS RETURNS EIGHT!!!
}
printf("%s", longest);
}
Why does it always return 8???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
C 中没有
string
数据类型。这是 C++ 吗?或者string
是一个 typedef?假设
string
是char *
的 typedef,您可能需要的是strlen
,而不是sizeof
。使用sizeof
得到的 8 实际上是指针的大小(指向字符串中的第一个字符)。There is no
string
data type in C. Is this C++? Or isstring
a typedef?Assuming
string
is a typedef forchar *
, what you probably want isstrlen
, notsizeof
. The 8 that you are getting withsizeof
is actually the size of the pointer (to the first character in the string).它把它当作一个指针,指针的大小在你的机器上显然是 8bytes = 64 位
It is treating it as a pointer, the sizeof a pointer is obviously 8bytes = 64 bits on your machine
这是字符数组本身和指向该数组开始位置的指针之间的常见错误。
例如,C 风格的字符串文字:
为 14 个字节(13 个字节用于消息,1 个字节用于空终止字符)。
您可以使用
sizeof()
来确定原始 C 样式字符串的大小。但是,如果我们创建一个指向该字符串的指针:
并尝试使用
sizeof()
查找它的大小,它只会返回系统上数据指针的大小。因此,换句话说,当您尝试从字符串库中获取字符串的大小时,您实际上只是获取了指向该字符串开头的指针的大小。您需要使用的是
strlen()
函数,它返回字符串的字符大小:希望这可以解决问题!
This is common mistake between the array of characters itself, and the pointer to where that array starts.
For instance the C-style string literal:
Is 14 bytes (13 for the message, and 1 for the null terminating character).
You can use
sizeof()
to determine the size of a raw C-style string.However, if we create a pointer to that string:
And attempt to find it's size with
sizeof()
, it will only always return the size of a data pointer on your system.So, in other words, when you try to get the size of the string from a string library, you're truly only getting the size of the pointer to the start of that string. What you need to use is the
strlen()
function, which returns the size of the string in characters:Hope this clears things up!
你说“不用担心这个->我写的lib”,但这是关键的信息,因为它定义了字符串。假设 string 是 char* 并且它在你的机器上的大小是 8。因此,sizeof(given[i]) 是 8,因为给定的 [i] 是一个字符串。也许您想要 strlen 而不是 sizeof。
You say "don't worry about this -> lib i wrote" but this is the critical piece of information, as it defines string. Presumably string is char* and the size of that on your machine is 8. Thus, sizeof(given[i]) is 8 because given [i] is a string. Perhaps you want strlen rather than sizeof.