【问题标题】:the size of my text file is 25 bytes, according to ftell. However, when I use ftell as the size of a char array, sizeof gives me 200. what gives? [closed]根据 ftell 的说法,我的文本文件大小为 25 个字节。但是,当我使用 ftell 作为 char 数组的大小时,sizeof 给了我 200。什么给了? [关闭]
【发布时间】:2020-03-18 01:00:02
【问题描述】:

本质上,这是我的问题。

FILE *fp = "/my/textfile/location";
fseek(fp, 0L, SEEK_END);
int size = ftell(fp);

char *output_string[size];

printf("%d", size); // gives me 25
printf("%ld", sizeof(output_string)); // gives me 200.

为什么当 ftell 返回的 int 是 20 时 sizeof(output_string) 给我 200?

【问题讨论】:

  • 200 == 25*8。 8 == sizeof(char*).
  • Jules - “如何 [动态] 声明 char 数组”可能有重复...如果您已经知道它只是“印刷错误”...
  • char* 数组不是 char 数组。
  • 你的意思是 *output_string[size] 是在创建一个指针数组吗?
  • FILE *fp = "/my/textfile/location"; -- 为你编译的?

标签: c arrays file io sizeof


【解决方案1】:

我真傻。

我不小心在我的数组声明中写了 *,这使得 output_string 成为一个指针数组,而不是字符

【讨论】:

    【解决方案2】:
    char *output_string[size];
    

    这是指针数组,如果你在 64 位机器上运行, 一个指针是 64 位的,即 8 个字节。 如果大小为 25,则:

    sizeof(output_string) = 25 * sizeof(char *) = 25 * 8 = 200
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-29
      • 1970-01-01
      • 2020-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多