C - > sizeof的字符串始终是8 [英] C -> sizeof string is always 8

查看:100
本文介绍了C - > sizeof的字符串始终是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 ???

why does it always return 8???

推荐答案

有没有字符串数据类型C.这是C ++?或者是字符串的typedef?

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

假设字符串的char * ,你可能想要的是的strlen的typedef ,而不是的sizeof 。您正在与获得的sizeof 8实际上是指针的大小(以字符串中的第一个字符)。

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).

这篇关于C - &GT; sizeof的字符串始终是8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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