C->字符串的大小始终为 8

发布于 2024-10-16 19:49:20 字数 610 浏览 6 评论 0原文

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

∞琼窗梦回ˉ 2024-10-23 19:49:20

C 中没有 string 数据类型。这是 C++ 吗?或者 string 是一个 typedef?

假设 stringchar * 的 typedef,您可能需要的是 strlen,而不是 sizeof。使用 sizeof 得到的 8 实际上是指针的大小(指向字符串中的第一个字符)。

There is no string data type in C. Is this C++? Or is string a typedef?

Assuming string is a typedef for char *, what you probably want is strlen, not sizeof. The 8 that you are getting with sizeof is actually the size of the pointer (to the first character in the string).

最初的梦 2024-10-23 19:49:20

它把它当作一个指针,指针的大小在你的机器上显然是 8bytes = 64 位

It is treating it as a pointer, the sizeof a pointer is obviously 8bytes = 64 bits on your machine

萌辣 2024-10-23 19:49:20

这是字符数组本身和指向该数组开始位置的指针之间的常见错误。

例如,C 风格的字符串文字:

char hello[14] = "Hello, World!";

为 14 个字节(13 个字节用于消息,1 个字节用于空终止字符)。
您可以使用 sizeof() 来确定原始 C 样式字符串的大小。

但是,如果我们创建一个指向该字符串的指针:

char* strptr = hello;

并尝试使用 sizeof() 查找它的大小,它只会返回系统上数据指针的大小。

因此,换句话说,当您尝试从字符串库中获取字符串的大小时,您实际上只是获取了指向该字符串开头的指针的大小。您需要使用的是 strlen() 函数,它返回字符串的字符大小:

sizeof(strptr); //usually 4 or 8 bytes
strlen(strptr); //going to be 14 bytes

希望这可以解决问题!

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:

char hello[14] = "Hello, World!";

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:

char* strptr = hello;

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:

sizeof(strptr); //usually 4 or 8 bytes
strlen(strptr); //going to be 14 bytes

Hope this clears things up!

拥醉 2024-10-23 19:49:20

你说“不用担心这个->我写的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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文