c - 如果数组大小只能是一个常量值,那么 char d_name[...] 是什么意思?

标签 c linux arrays

如果数组大小只能是一个常量值,那么

   char d_name[...] 

是什么意思?

实际上,在dirent.h 文件中声明了一个struct dirent。它的声明如下:

struct dirent{
  ....
  ino_t d_ino;
  char d_name[...];
  ...
  };

它用于一次读取一个目录内容,即 inode 号和文件名等...

我的意思是这样一个数组的最大大小是多少,一旦定义了这样一个数组,在内存中静态分配了多少空间?这样的定义可移植吗?

最佳答案

假设它来自struct linux_dirent,它实际上是char d_name[] :

struct linux_dirent {
    unsigned long  d_ino;     /* Inode number */
    unsigned long  d_off;     /* Offset to next linux_dirent */
    unsigned short d_reclen;  /* Length of this linux_dirent */
    char           d_name[];  /* Filename (null-terminated) */
}

它被称为灵活的数组成员,使用 malloc 您可以为结构分配更多内存,从而为 d_name 提供可变大小。

编辑

OP 引用的文本:

Directory entries are represented by a struct dirent

struct dirent {
    ...
    ino_t d_ino;            /* XSI extension --- see text */
    char  d_name[...];      /* See text on the size of this array */
...
};

对于 ...,作者表示大小不是固定的按照标准。每个实现都必须选择一个固定大小,例如 Linux 选择 256。但它不是有效代码。

关于c - 如果数组大小只能是一个常量值,那么 char d_name[...] 是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9645472/

相关文章:

c - OSX 上 C OpenGL 程序的基本框架

c - C 中的隐式 "declaration-in-instantiation"?

c - 错误 C4996 : visual studio: why do I get an error when I use fopen in c?

为已经退出的线程调用 pthread_detach?

linux - 从源代码构建软件比从包管理器安装它们有什么优势吗?

node.js - bash:npm:在 Debian 9 中找不到命令

linux - 如何读取数据文件列表以便在 GNUplot 中使用变量?

java:如何将二维数组拆分为两个二维数组

python - 在 Python 中返回数组中最大元素的索引

c++ - 将动态二维数组在 C++ 和 Fortran 之间传递