#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 ???

最佳答案

C中没有string数据类型。这是C ++吗?还是string是typedef?

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

10-08 02:02